Deleting files on a shared server

Discussion in 'Python' started by Josh English, Oct 7, 2011.

  1. Josh English

    Josh English Guest

    This is a follow-up to some questions I posted a month or two ago. I have two programs running on various Windows XP boxes, sharing several resource files on a Windows 2003 server. It's a mapped drive on the workstations to a shared folder.

    I am using a locking utility that works by creating ".lock" files in the shared folder and deleting those files when the program is done with them.

    To delete the files, I am using os.unlink.

    One lock file refuses to disappear, even though I have code at both application startup and shutdown (on the OnInit and OnExit methods to the wxPython Application object) that hunts down .lock files and deletes them.

    Is there a better command than os.unlink to delete a file on Windows 2003 server?

    Josh
    Josh English, Oct 7, 2011
    #1
    1. Advertising

  2. Josh English wrote:

    > This is a follow-up to some questions I posted a month or two ago. I have
    > two programs running on various Windows XP boxes, sharing several resource
    > files on a Windows 2003 server. It's a mapped drive on the workstations to
    > a shared folder.
    >
    > I am using a locking utility that works by creating ".lock" files in the
    > shared folder and deleting those files when the program is done with them.
    >
    > To delete the files, I am using os.unlink.


    How and when? If you are deleting the files using a __del__ handler in an
    instance, it is quick possible that it is never being run, or not being run
    when you think it is.

    For file locking, you should consider using a portable solution like this
    one:

    http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/


    > One lock file refuses to disappear, even though I have code at both
    > application startup and shutdown (on the OnInit and OnExit methods to the
    > wxPython Application object) that hunts down .lock files and deletes them.


    Perhaps the file is open and so can't be deleted under Windows. Are you
    getting an exception when you try to unlink the file? If so, what does it
    say?


    > Is there a better command than os.unlink to delete a file on Windows 2003
    > server?


    No. os.unlink is a wrapper around your system's unlink command -- if it
    can't delete the file, you can't delete the file.


    --
    Steven
    Steven D'Aprano, Oct 7, 2011
    #2
    1. Advertising

  3. Josh English

    Josh English Guest

    The problem shows up when the application starts. It tries to read the file but the lock mechanism times out because the file is still around after the last time the application ran.

    It's a wxPython program. The code to unlink the .lock files is run in the wxApp.OnInit method (before any code to open these resources) and in the wxApp.OnExit method. I know both of these methods are being called.

    The locking mechanism I am using can be found at http://www.evanfosmark.com/2009/01/cross-platform-file-locking-support-in-python/

    The clearing code is:

    import os
    import fnmatch
    files = fnmatch.filter(os.listdir(self.Options.DataDir), "*.lock")
    for f in files:
    os.unlink(os.path.abspath(os.path.join(self.Options.DataDir, f)))

    The Options object has a property called DataDir.

    MMM...

    Now that I sit down to test abso-frikkin'-lutely that this code does what I want it to do, it appears not to do this at all. The files list I build doesn't work and returns an empty list. I may have found a workaround using glob.

    Now my face is red.
    Josh English, Oct 7, 2011
    #3
  4. Josh English

    Tim Golden Guest

    On 07/10/2011 02:14, Josh English wrote:
    > This is a follow-up to some questions I posted a month or two ago. I
    > have two programs running on various Windows XP boxes, sharing
    > several resource files on a Windows 2003 server. It's a mapped drive
    > on the workstations to a shared folder.
    >
    > I am using a locking utility that works by creating ".lock" files in
    > the shared folder and deleting those files when the program is done
    > with them.
    >
    > To delete the files, I am using os.unlink.
    >
    > One lock file refuses to disappear, even though I have code at both
    > application startup and shutdown (on the OnInit and OnExit methods to
    > the wxPython Application object) that hunts down .lock files and
    > deletes them.


    Assuming that your code paths succeed and that the unlink actually
    happens, it is possible for files to continue to exist after they
    have been successfully deleted. This happens if another process
    has opened them with share-delete mode; typically this will be
    a virus checker or a process like the TortoiseSVN cache (or its
    counterparts for other VCS). The file won't actually disappear
    until the last handle on it is released.

    TJG
    Tim Golden, Oct 7, 2011
    #4
  5. En Fri, 07 Oct 2011 04:45:32 -0300, Tim Golden <>
    escribió:
    > On 07/10/2011 02:14, Josh English wrote:


    >> To delete the files, I am using os.unlink.
    >>
    >> One lock file refuses to disappear, even though I have code at both
    >> application startup and shutdown (on the OnInit and OnExit methods to
    >> the wxPython Application object) that hunts down .lock files and
    >> deletes them.

    >
    > Assuming that your code paths succeed and that the unlink actually
    > happens, it is possible for files to continue to exist after they
    > have been successfully deleted. This happens if another process
    > has opened them with share-delete mode; typically this will be
    > a virus checker or a process like the TortoiseSVN cache (or its
    > counterparts for other VCS). The file won't actually disappear
    > until the last handle on it is released.


    In such cases the openfiles command [1] is very useful for detecting who
    is holding the file open.

    [1]
    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/openfiles.mspx

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 8, 2011
    #5
    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. DJ Dev
    Replies:
    3
    Views:
    16,095
    Gandalf
    Feb 8, 2004
  2. Hrvoje Vrbanc
    Replies:
    0
    Views:
    334
    Hrvoje Vrbanc
    Feb 25, 2004
  3. Simon
    Replies:
    1
    Views:
    425
    =?Utf-8?B?dGlta2xpbmc=?=
    Nov 10, 2005
  4. Harry Barker
    Replies:
    2
    Views:
    505
    Alf P. Steinbach
    Apr 19, 2006
  5. crea
    Replies:
    2
    Views:
    397
    Nobody
    Dec 28, 2012
Loading...

Share This Page