Determining if a file is locked in Windows

elake ericlake at gmail.com
Thu Oct 19 16:44:32 CEST 2006


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

> > http://groups.google.com/group/comp.lang.python/browse_thread/thread/d3dee5550b6d3652/ed00977acf62484f?lnk=gst&q=%27copying+locked+files%27&rnum=1
> >
> > The problem is that I have a script that can find the pst files on
> > every machine in my network and back them up to a server for safe
> > keeping. The problem is that when Outlook is running it locks the file
> > and will not allow me to copy it to the destination. I am using the
> > shutil module for the copy.
> >
> > Is there a way to first determine if the file is locked and then do the
> > copy if it isn't? I thought about looking to see if Outlook.exe is
> > running but the machines are shared and the process could be running
> > but with a different pst file in use.
> >
> > Thanks in advance
> >
> Try the copy and catch the exception instead.
>
> -Larry Bates

Larry thanks for your suggestion. this is what I tried:

#!/usr/bin/env python

import os, shutil

path = 'c:\documents and settings\username\Local Settings\Application
Data\Microsoft\Outlook'

src = 'Outlook.pst'
dst = 'test.pst'

os.chdir(path)

try:
    shutil.copy2(src, dst)
except IOError:
    print 'Must be locked by Outlook'

print 'Finished'

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.

Is there another way to do this that I am missing. I am still kind of
new to Python. If i could tell that outlook had the file locked before
I tried the copy then I think that it would be prevented.




More information about the Python-list mailing list