[Tutor] Locking a file in linux with Python

Pat Martin wpmartin at gmail.com
Wed Feb 22 20:18:26 CET 2006


On 2/22/06, Pat Martin <wpmartin at gmail.com> wrote:
>
> > I am trying to find on the web how to lock a file with python in Linux.
> > > I am trying to process a group of files that are continually being
> > > written to and when I am working on one file I would like to be able
> > to
> > > lock it. Can anyone point me in the right direction for that.
> >
> > Hi Pat,
> >
> > Take a look at the 'portalocker' recipe in the Python Cookbook:
> >
> >     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
> >
> > Does this address your question?
> >
> >
> >
> > Good luck to you!
> >
>
> I had actually looked at that site, but when I do the commands
>
> testfile = open('testlock', 'a+')
> fcntl.flock(mboxfile.fileno(), fcntl.LOCK_EX)
>
> in the interpreter and then try to access the file in another terminal (I
> leave the interpreter open) I can write to the file just fine with vi. So I
> am not thinking it is working. Unless I am missing something...
>
> Thanks again.
>
> Pat Martin


Ooops the commands I am running are actually

mboxfile = open('testlock', 'a+')
fcntl.flock(mboxfile.fileno(), fcntl.LOCK_EX)

and it is not working. The two filenames do match, sorry for that typo
earlier.
--
Pat Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060222/6a8bd085/attachment.htm 


More information about the Tutor mailing list