Determining if a file is locked in Windows

Ricardo Reyes chiquito at
Fri Oct 20 18:08:15 CEST 2006

elake wrote:
> Larry Bates wrote:
> > elake wrote:
> > > I found this thread about a pst file in Windows being locked and I am
> > > having the same issue.
> > >

> The problem is that even though I catch the IOError it overwrites the
> dst file and makes it 0kb. This is going to be for backing these files
> up and it wont be good to overwrite the backup with a bad copy.

You can try to copy to a dst file that is not the backup file, but
instead it's in a temp location or with a temp name.

If the copy fails with IOError, just delete the new 0 bytes temp file.
If the copy doesn't fail, delete the old backup, rename the new temp
file to the destination name, and you are done. The rename part
shouldn't fail since you are operating in a new file that's not locked.
And for extra safety, don't erase the old backup before renaming the
new one, but rename to .bak.old or something like that.

Good luck.


More information about the Python-list mailing list