473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 7685
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Francisco Miguel Montenegro Montes | last post by:
Hi, perhaps some of you can help me... I'm installing Python 2.3 (in Linux RedHat 8.0) and I need to build it like a shared library, because I want to interact Python with PostgreSQL. Following...
2
by: Douglass Turner | last post by:
Hi, Please release me from my own private hell. Platform: SuSE 8.1 I'm installing python 2.3 tarball as follows: ../configure --enable-shared make
14
by: S Green | last post by:
Does any one now if a shared memory module exists, written in python for a windows platform. I now one exists for unix? help most appreciated, S Green
1
by: Srijit Kumar Bhadra | last post by:
Hello, Here are code snippets to create and access shared memory in Python with and without ctypes module. With regards, Srijit Filename : SharedMemCreate.py import msvcrt, mmap
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
6
by: EP | last post by:
I need to build a fairly simple application that will reside on remote storage, not on a server, and I am looking for any best practices and approaches that have worked for others. I believe...
5
by: Tim | last post by:
Hello Everyone, I am getting shared memory in python using the following. szName = c_char_p(name) hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, None, PAGE_READONLY, 0,...
0
by: abarun22 | last post by:
HI I am new to SWIG & Python and right now i am in the process of wrapping some "C" functionalities present in a static library for python. I do have my C file "name.c" which just contains some...
4
by: stuntgoat | last post by:
Hi, I want to start using Python 2.6 and 3000. I have several questions. What, in your experiences, is a functionally elegant solution to installing 2.6 and 3 from source without breaking...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.