Thread: force overwrite

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Rep Power

    force overwrite

    In a script I have, I need to be able to overwrite a file that is at times in use. Is there any way in python, or dos commands for that matter, that I can force a delete or force an overwrite of a file that is in use?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Hull, UK
    Rep Power
    A file cant be opened for writing while another process has a lock on that file. You should note than Python doesn't apply any file locking by default .

    So you have two options.. remove the file lock or wait for the file to be freed.

    You might want to take a look at these recipes for locking/unlocking files form Python.

    There are also a module available in *nix for locking and unlocking files called fcntl.

    The second option is probly the best since its the most Pythonic and thus platform independent. Its also surprisingly simple, not to mention safer .

    #!/usr/bin/env python
    from time import sleep
    while True:
            file('some.txt', 'w')
        except IOError:
    In this example the file is tested to see if it is available untill it is; sleeping for one second before trying again. This could be writen better depending on the rest of your program .

    Hope this helps,

    Last edited by netytan; June 19th, 2004 at 10:35 AM. Reason: correction
    programming language development: Hula

IMN logo majestic logo threadwatch logo seochat tools logo