how to lock a file in ftp site

andy andy at wild-flower.co.uk
Mon Dec 19 18:28:34 EST 2005


muttu2244 at yahoo.com wrote:

>hi all
>
>am trying to write some information into the file, which is located in
>ftp, and this file can be updated by number of people, but if at all i
>download a file from the ftp to my local machine, update it and then
>upload it back to ftp, and at the same time if some one else downloads
>the same file for the modification, then the data will be overwritten.
>
>so is there a way in Python script where i can lock the file, so that
>no one updates it until i release the lock.
>
>Or is there a way where i can directly update the file from the ftp
>itself, i mean without downloading it to my local machine.
>
>Thanks in advance for the help
>
>regards
>yogi
>
>  
>
I've come across this problem - instead of uploading the file to it's

intended directory, upload it to a temp directory, with a unique name
(say uuuuuyyyymmddhhmmss where uuuuu is username and the rest are
obvious) and then *rename* it to it's correct name in the real
destination directory. Do the move in a try... except block and if it
fails, take evasive action.

In FTP a rename (i.e. move) is usually an atomic action but an upload is
not. You have an unavoidable race condition here trying to lock a file
(even if ftp allowed it) but with the method described above, you can
avoid it, but get the same effect.

Hope that helps!
-andyj




More information about the Python-list mailing list