Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 18th, 2004, 09:32 AM
Datinky1 Datinky1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 Datinky1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old June 18th, 2004, 11:24 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

http://aspn.activestate.com/ASPN/Co...n/Recipe/252495
http://aspn.activestate.com/ASPN/Co...on/Recipe/65203

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

http://python.org/doc/2.3.4/lib/module-fcntl.html

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

Code:
#!/usr/bin/env python

from time import sleep

while True:
    try:
        file('some.txt', 'w')
    except IOError:
        time.sleep(1)


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,

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : June 19th, 2004 at 10:35 AM. Reason: correction

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > force overwrite


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT