[Tutor] locking files

Vishnu Mohan vishnu at montalvosystems.com
Thu Jun 14 16:05:52 CEST 2007


You can look into the flock or lockf  methods in fcntl module
(or)
the following link will help you

http://www.python.org/doc/2.4/lib/module-fcntl.html

VishnuMohan


Alan Gauld wrote:
> "Jeff Peery" <jeffpeery at yahoo.com> wrote
>
>   
>> does anyone know if there is a way in python to lock 
>> a file so others cannot open it, or can only read it? 
>>     
>
> Thats normally controlled by your OS and the rules vary 
> slightly between them. In general if you open a file for 
> writing the OS should lock it against writing (and in 
> some cases against reading too!). So if you want to 
> lock an existing file while you read and write you could use 
> a mode string of 'r+'. If you want to create a new locked 
> file use 'w+''. BUT remember that its up to you to manage 
> where the cursor is, otherwise you could wind up overwriting 
> your data.
>
> Another way to protect access, but less reliable, is 
> to change the file permissions (using os.chmod()) 
> at the start of the operation and change themback at 
> the end. But that will only protect ahainst access by 
> other users not against access by other programs that 
> you might be running! Also you need to have 
> permission to change permissions in the first place!
>
> HTH,
>
>
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070614/c452127a/attachment-0001.htm 


More information about the Tutor mailing list