Re: Can't get exclusive file lock when safely renaming a file

2008-12-06 Thread Jeffrey Straszheim
Steven D'Aprano wrote: I'm trying to safely rename a file without over-writing any existing files, and I've run into a problem with file locks. Here's a naive way of renaming without over-writing By default on a Linux filesystem, flock() gives you an _advisory_ lock. Other processes can touch

Re: Can't get exclusive file lock when safely renaming a file

2008-12-06 Thread Steve M
On Dec 6, 12:25 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > The rename works, but here is my problem: after getting what I thought > was an exclusive lock on the new file, but before calling os.rename(), I > can still over-write it from another process: > > $ echo "this com

Re: Can't get exclusive file lock when safely renaming a file

2008-12-06 Thread Martin P. Hellwig
Steven D'Aprano wrote: import os, fcntl oldname = "ham.txt" newname = "spam.txt" def lock_destination(name): fileno = os.open(name, os.O_CREAT | os.O_EXCL) fcntl.flock(fileno, fcntl.LOCK_EX) # POSIX systems only return fileno # Create a test file to be renamed. f = open(oldname, '