Newbie Q: File accessing

Fernando Pérez fperez528 at yahoo.com
Wed Jun 19 17:00:51 EDT 2002


Todd Harris wrote:

> Hi, I apologize if this question has been beaten to death or if there's just
> something that I haven't been seeing, but here's my problem (with a little
> bit of background):
> 
> I have a file out on the network somewhere that stores info as text. I'm
> writing a Python script that updates that file. However, it's a multi-user
> application, and so my problem is that one user's script may be in the
> middle of writing to that file while another user's script is attempting to
> read that file or <even worse> writing to that file as well.
> 
> For those of you who use Microsoft <gulp...flame shield on> Word, when a
> document is opened by one user, and another user tries to access it, they
> can open it for read only. Obviously, Word has made some file attribute
> where it is locked to that first user, and only that first user can write to
> it while they have it open. Is there a way to do this in Python? Or what
> other possible options do I have? I've looked been using the open(
> filePath ) function as well as the os.open( ) function and os.access( ) to
> check my progress, but can't seem to figure out how to get around this.
> Thanks,
> 
> Todd

Here's a basic discussion to get you started:

http://trixie.triqs.com/pipermail/metakit/2001-December/000439.html

In summary, getting this to work portably cross-platform is highly 
non-trivial. If you need only an in-house  specific solution under 
well-specified constraints, you may be able to come up with a homebrewed 
option which works well for you. Simple file.lock locks with some process 
info in them may be enough in your environment.

But this recipe may be enough:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203


For more, just google(python file locking).

Good luck,

f.



More information about the Python-list mailing list