470,600 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,600 developers. It's quick & easy.

shared file access in python

Does Python allow to open files in shared mode like fsopen or _open
do? If not how to make it happen?
Jul 18 '05 #1
8 7200
Lev Elblert wrote:
Does Python allow to open files in shared mode like fsopen or _open
do? If not how to make it happen?


Probably the "os" module's version of open() will do what you
need: http://docs.python.org/lib/os-fd-ops.html, assuming the
standard builtin open() is not doing what you need.

-Peter
Jul 18 '05 #2
The file() constructor probably takes any second argument that your OS's
fopen() does, though this may have changed with the creation of
universal newlines.

If there's something that os.open() can do that file() can't, you
should be able to achieve it by using os.fdopen(): (untested)
def magic_file(filename, flag=os.O_RDONLY, mode='r', bufsize=None):
fd = os.open(filename, flag)
return os.fdopen(fd, mode, bufsize)

I don't know what _open or fsopen are.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA02HkJd01MZaTXX0RAuEjAJ9Sb67Ll/Jbwel3Uh0lnA2NOzUf3wCZASWv
HwxkT3vLaZ1b+8QmcKp+mKg=
=aBvF
-----END PGP SIGNATURE-----

Jul 18 '05 #3
On Sat, 18 Jun 2004, Lev Elblert wrote:
Does Python allow to open files in shared mode like fsopen or _open
do? If not how to make it happen?


Assuming you're on a Win32 platform, I think the msvcrt module has what
you're looking for. The standard file object supports only the standard C
library file stream API.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

Jul 18 '05 #4
Andrew MacIntyre <an*****@bullseye.apana.org.au> wrote in message news:<ma***********************************@python .org>...
On Sat, 18 Jun 2004, Lev Elblert wrote:
Does Python allow to open files in shared mode like fsopen or _open
do? If not how to make it happen?


Assuming you're on a Win32 platform, I think the msvcrt module has what
you're looking for. The standard file object supports only the standard C
library file stream API.


Thanks all!

1. os.open flags parameter tells creation disposition (read-only,
executable(unix)) etc, not shared.

2. msvcrt.lib does have a lot of functions, but not msvcrt module in
Python. (correct me if I'm wrong)

3. I created and extension:
/*=====================*/

#include "Python.h"
#include <share.h>

static PyObject *
Dofsopen(PyObject *self, PyObject *args)
{
char *FileName = NULL;
char *Mode = NULL;
char * Share = NULL;

int ShareFlag = _SH_DENYNO;
FILE *f;
PyObject *FileObject;
if (!PyArg_ParseTuple(args, "sss", &FileName, &Mode, &Share))
return NULL;
if (strcmp(Share, "") == 0) // allow all
ShareFlag = _SH_DENYNO;
else if (strcmp(Share, "r") == 0) // deny read
ShareFlag = _SH_DENYRD;
else if (strcmp(Share, "w") == 0) // deny write
ShareFlag = _SH_DENYWR;
else if (strcmp(Share, "rw") == 0) // deny read/write
ShareFlag = _SH_DENYRW;
f = _fsopen(FileName, Mode, ShareFlag);
if (!f)
{
PyErr_SetFromErrno(PyExc_Exception);
return NULL;
}
FileObject = PyFile_FromFile(f, FileName, Mode, fclose);
return FileObject;
}

static PyMethodDef fsopen_methods[] = {
{"fsopen", Dofsopen, METH_VARARGS, "fsopen() doc string"},
{NULL, NULL}
};

void
initfsopen(void)
{
Py_InitModule("fsopen", fsopen_methods);
}

/*=====================*/

This extension returnns a Python file object or rases an exception.
Also MS docs say, that fsopen is ANSI C function. I do not have access
to unix machine, so can't check it.

Thanks again.
Jul 18 '05 #5
Lev Elblert wrote:
2. msvcrt.lib does have a lot of functions, but not msvcrt module in
Python. (correct me if I'm wrong)


http://docs.python.org/lib/module-msvcrt.html shows there are a variety
of functions there (try typing "import msvcrt" as well), but I can't
see that they have what you need.

-Peter
Jul 18 '05 #6
On Mon, 21 Jun 2004 10:28:24 -0400, Peter Hansen <pe***@engcorp.com>
declaimed the following in comp.lang.python:
Lev Elblert wrote:
2. msvcrt.lib does have a lot of functions, but not msvcrt module in
Python. (correct me if I'm wrong)
http://docs.python.org/lib/module-msvcrt.html shows there are a variety
of functions there (try typing "import msvcrt" as well), but I can't
see that they have what you need.


Somehow, I suspect one would have to using the win32 modules for
the desired functionality:

From the ActivePython win32 documentation:
"""
win32file.CreateFile
PyHANDLE = CreateFile(fileName, desiredAccess , shareMode , attributes ,
creationDisposition , flagsAndAttributes , hTemplateFile )

Creates or opens the a file or other object and returns a handle that
can be used to access the object.
"""

Of course, this then means one also has to use the win32file.*
routines for all the I/O. Though maybe the msvcrt module
open_osfhandle() can then be used -- pass it the handle from
win32file.CreateFile(), pass that result to os.fdopen(), etc... and use
the rest of the msvcrt routines.

Doesn't common Linux practice rely upon creating a lock-file
first, to prevent shared access to a file? That would Linux/UNIX doesn't
really have "exclusive access" control on files. So I wouldn't expect to
find any support for such in anything using a Posix compatible I/O
system.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7
Peter!

I do not know what kind of msvcrt module you have, but here is the
output of relavant commands on my machine:
import msvcrt
str(msvcrt) "<module 'msvcrt' (built-in)>" print msvcrt.__doc__ None dir (msvcrt) ['LK_LOCK', 'LK_NBLCK', 'LK_NBRLCK', 'LK_RLCK', 'LK_UNLCK', '__doc__',
'__name__', 'get_osfhandle', 'getch', 'getche', 'heapmin', 'kbhit',
'locking', 'open_osfhandle', 'putch', 'setmode', 'ungetch']


out of all functions, probably kbhit and getch are used, to control
multythereaded applications from console.

Peter Hansen <pe***@engcorp.com> wrote in message news:<Zc********************@powergate.ca>... Lev Elblert wrote:
2. msvcrt.lib does have a lot of functions, but not msvcrt module in
Python. (correct me if I'm wrong)


http://docs.python.org/lib/module-msvcrt.html shows there are a variety
of functions there (try typing "import msvcrt" as well), but I can't
see that they have what you need.

-Peter

Jul 18 '05 #8
Lev Elblert wrote:
Peter!

I do not know what kind of msvcrt module you have, but here is the
output of relavant commands on my machine: [snip]

You misread the message below, I believe. You seem to think I was
the one saying that the msvcrt module did not have lots of functions...

Also, please don't top-post. Thank you.

-Peter
Peter Hansen <pe***@engcorp.com> wrote in message news:<Zc********************@powergate.ca>...
Lev Elblert wrote:
2. msvcrt.lib does have a lot of functions, but not msvcrt module in
Python. (correct me if I'm wrong)


http://docs.python.org/lib/module-msvcrt.html shows there are a variety
of functions there (try typing "import msvcrt" as well), but I can't
see that they have what you need.

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Francisco Miguel Montenegro Montes | last post: by
14 posts views Thread by S Green | last post: by
1 post views Thread by Srijit Kumar Bhadra | last post: by
5 posts views Thread by Tim | last post: by
reply views Thread by abarun22 | last post: by
4 posts views Thread by stuntgoat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.