Determining if a file is locked in Windows

Discussion in 'Python' started by elake, Oct 18, 2006.

  1. elake

    elake Guest

    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...2484f?lnk=gst&q='copying locked files'&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
     
    elake, Oct 18, 2006
    #1
    1. Advertising

  2. elake

    Larry Bates Guest

    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...2484f?lnk=gst&q='copying locked files'&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 Bates, Oct 18, 2006
    #2
    1. Advertising

  3. elake

    elake Guest

    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...2484f?lnk=gst&q='copying locked files'&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.
     
    elake, Oct 19, 2006
    #3
  4. 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.
    > > >

    >
    > > > http://groups.google.com/group/comp...2484f?lnk=gst&q='copying locked files'&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.


    maybe try and open the file for reading first, then if it opens ok,
    just close it and do the copy?
     
    MatthewWarren, Oct 19, 2006
    #4
  5. elake

    elake Guest

    MatthewWarren wrote:
    > 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.
    > > > >

    > >
    > > > > http://groups.google.com/group/comp...2484f?lnk=gst&q='copying locked files'&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.

    >
    > maybe try and open the file for reading first, then if it opens ok,
    > just close it and do the copy?


    I tried to do that and it did let me open it without an error. Here is
    what I have done now and it seems work.

    def copyFile(src, dst):
    if os.path.isfile(dst):
    shutil.copy2(dst, dst_bak)
    try:
    shutil.copy2(src, dst)
    except IOError:
    if os.path.isfile(dst_bak):
    shutil.copy2(dst_bak, dst)
    os.remove(dst_bak)
    else:
    try:
    shutil.copy2(src, dst)
    except IOError:
    if os.path.isfile(dst_bak):
    shutil.copy2(dst_bak, dst)

    It check to see if the dst file is there first and them makes a backup
    of it first. That way if the copy goes bad then there is still a backup
    of it. Do you see anywhere that I could have done this
    better/differently?
     
    elake, Oct 19, 2006
    #5
  6. 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.

    Ricardo
     
    Ricardo Reyes, Oct 20, 2006
    #6
  7. 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.

    Ricardo
     
    Ricardo Reyes, Oct 20, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    1
    Views:
    2,458
  2. Victor Bazarov
    Replies:
    1
    Views:
    487
    Vladimir Jovic
    Aug 25, 2009
  3. red floyd
    Replies:
    0
    Views:
    779
    red floyd
    Aug 25, 2009
  4. Chris Withers

    python install on locked down windows box?

    Chris Withers, Sep 22, 2011, in forum: Python
    Replies:
    4
    Views:
    2,009
    Chris Withers
    Sep 23, 2011
  5. Ali Akhavan

    Detect file is locked - windows

    Ali Akhavan, Nov 14, 2012, in forum: Python
    Replies:
    8
    Views:
    269
Loading...

Share This Page