Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why Can't I Delete a File I Created with Win XP?

Reply
Thread Tools

Why Can't I Delete a File I Created with Win XP?

 
 
W. eWatson
Guest
Posts: n/a
 
      12-05-2009
I'm trying to store analytic data in a folder called Analysis. If the
user doesn't have the folder, I make one for him, and then write a txt
file into it. In this case a histogram of values, x and frequency.
However, it appears that I made a mistake somewhere and cannot delete it
using the Win XP delete. Here's the code that determines if Analysis
exists, and whether to create it.

print "Path for Histogram", self.current_path
s = self.current_path
s = "Analysis"
s = os.path.join("Analysis",s)
print "s joined ",s <------------- debug print
if not os.path.exists("Analysis"):
os.mkdir("Analysis")
f = file( s, "wb")
if not f:
self.LogError( "Histogram file creation error 1" )
return False

In writing the code to handle all of this, I forgot to write the txt
folder to the file, nor did I close it. It was someone else's code, and
I got caught up in the creation of Analysis. Here's what XP tells me
when I try to delete the folder.

Very funny. I just tried it to get the content of the msg, and XP
deleted it. Let me recreate it and try again. OK, here it is:

Error Deleting Folder or File.
Cannot delete Analysis: It is being used by another person or program.
Close programs that might be using it and try again.

There is no file created, just the folders Analysis\Analysis. One too
many. The second Analysis shows as an icon for a file of size 0KB.

I printed with the debug print above:
Path for Histogram Events\v20070206_055012.06.dat
s joined Analysis\Analysis
Analysis\Analysis should only be Analysis.
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      12-05-2009
On Dec 5, 9:57*pm, "W. eWatson" <(E-Mail Removed)> wrote:
[snip]
> * * * * *s = self.current_path

s referred to something ...
> * * * * *s = "Analysis"

but now s refers to "Analysis" ... at best, there is redundant &
confusing code; at worst, the source of your problem.

> * * * * *s = os.path.join("Analysis",s)

and now s refers to r"Analysis\Analysis" (on your platform)
> * * * * *print "s joined ",s * *<------------- debug print


[snip]

> There is no file created, just the folders Analysis\Analysis. One too
> many. The second Analysis shows as an icon for a file of size 0KB.
>
> I printed with the debug print above:
> * *Path for Histogram Events\v20070206_055012.06.dat
> * *s joined *
> Analysis\Analysis should only be Analysis.


Huh?? s = os.path.join("fubar", "fubar") should produce r"fubar
\fubar" (as documented) ... If you don't want s to refer to r"Analysis
\Analysis", then quite simply don't do that!

 
Reply With Quote
 
 
 
 
W. eWatson
Guest
Posts: n/a
 
      12-05-2009
The program code is not mine, but I wanted to modify it to produce an
Analysis folder when the user wants histogram file, basically, a txt
file to appear in Analysis.

Elsewhere in the program this is done for another type of data that is
directed to an Events folder. I figured I could copy the code from there
and use it here. Here's other code for Events.
# He's building a time stamp for the file name.
# I'll use this with a slight modification to the name in
# my code
millisecs = int((event_time - int(event_time))*100)
s = "v%4d%02d%02d_%02d%02d%02d.%02d.dat" % (
t.tm_year, t.tm_mon, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, millisecs )
#OK, he's got the file name assembled
s = os.path.join("Events",s)
if not os.path.exists("Events"):
os.mkdir("Events")
f = file( s, "wb" )
if not f:
self.LogError( "File creation error 1" )
return False

I caused the redundancy by just changing the meaning of s with a new
statement below it. I should have commented the first one out. Yeah, I
probably screwed up here and should have and,for purposes of debugging,
just used another file name like s="ADATAFILE2009_mmdd.txt", which does
not exist at this point of the coding stage. So I should have produced
Analysis\ADATAFILE2009_mmdd.txt. If I had done that then I would have
ended up with an empty file in the Analysis folder. However, even at
that, why can't I delete this empty file called Analysis?

When I said I was able to get rid of the "file", it's probably because I
had rebooted at some time after I puzzled over the problem, and now OS
somehow released it's grip, that is, whatever process was trying to use
the "file" too.


John Machin wrote:
> On Dec 5, 9:57 pm, "W. eWatson" <(E-Mail Removed)> wrote:
> [snip]
>> s = self.current_path

> s referred to something ...
>> s = "Analysis"

> but now s refers to "Analysis" ... at best, there is redundant &
> confusing code; at worst, the source of your problem.
>
>> s = os.path.join("Analysis",s)


> and now s refers to r"Analysis\Analysis" (on your platform)
>> print "s joined ",s <------------- debug print

>
> [snip]
>
>> There is no file created, just the folders Analysis\Analysis. One too
>> many. The second Analysis shows as an icon for a file of size 0KB.
>>
>> I printed with the debug print above:
>> Path for Histogram Events\v20070206_055012.06.dat
>> s joined
>> Analysis\Analysis should only be Analysis.

>
> Huh?? s = os.path.join("fubar", "fubar") should produce r"fubar
> \fubar" (as documented) ... If you don't want s to refer to r"Analysis
> \Analysis", then quite simply don't do that!
>

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-05-2009
On Dec 6, 2:46*am, "W. eWatson" <(E-Mail Removed)> wrote:

[snip]

> * * * * *f = file( s, "wb" )
> * * * * *if not f:
> * * * * * * *self.LogError( "File creation error 1" )
> * * * * * * *return False


Either you are shadowing the built-in function file() or you haven't
tested this code ... file() aka open() returns a file object (which
cannot have a "false" value) or it raises an exception; the
self.LogError() call can never be executed.


> I caused the redundancy by just changing the meaning of s with a new
> statement below it. I should have commented the first one out. Yeah, I
> probably screwed up here and should have and,for purposes of debugging,
> just used another file name like s="ADATAFILE2009_mmdd.txt", which does
> not exist at this point of the coding stage. So I should have produced
> Analysis\ADATAFILE2009_mmdd.txt. If I had done that then I would have
> ended up with an empty file in the Analysis folder.


Ever heard the phrase "need to know"?

> However, even at
> that, why can't I delete this empty file called Analysis?


Are you trying to delete the file from another command window while
Python is paused at the interactive prompt? In any case describe how
you are trying to delete the file. Fire up another command window, cd
to whatever is the current working directory for your script, do a dir
command, and copy/paste the RELEVANT parts (including all directory
names). Please don't top-post, and before you hit the send button,
please delete any text that is more appropriate to your memoirs than a
problem description.
 
Reply With Quote
 
J
Guest
Posts: n/a
 
      12-06-2009
On Sat, Dec 5, 2009 at 17:42, John Machin <(E-Mail Removed)> wrote:
> On Dec 6, 2:46*am, "W. eWatson" <(E-Mail Removed)> wrote:
>> However, even at
>> that, why can't I delete this empty file called Analysis?

>
> Are you trying to delete the file from another command window while
> Python is paused at the interactive prompt? In any case describe how
> you are trying to delete the file. Fire up another command window, cd
> to whatever is the current working directory for your script, do a dir
> command, and copy/paste the RELEVANT parts (including all directory
> names). Please don't top-post, and before you hit the send button,
> please delete any text that is more appropriate to your memoirs than a
> problem description.


I'm no expert in Python, so YMMV and all that, but sounds like, as was
mentioned before, that the file isn't being properly closed. I was
under the impression that the interpreter would close any open
filehandles as part of garbage collection as the script ends and the
interpreter closes out... but it's good practice to explicitly close
them in any case instead of just assuming that the interpreter or os
will do it for you... applicable to any language, not just python.

But that being said, this brings to mind a question about this... in
*nix, when I can't do something like delete a file or directory, or
unmount a filesystem and cant find the cause, I can do an lsof and
grep for references to the file/directory in question, then work from
there to close anything that still has a lock.

Most all of the development work I've ever done (other than C++ and
VB.Net) has been done on Linux systems. And honestly, most of my C++
stuff was written/compiled on Linux systems too... so I'm not that
experienced with developing applications on windows. Does Windows (XP
or later) have an lsof or lsof type tool to show who/what has a lock
on a file or folder? I've not found anything like that so far, but as
I said, I'm not much of a windows user anyway.

I'm at the point where I can actually write useful code in Python, and
not just hack my way through to a solution, so this is somethign that
would be useful for me to know. I'm about to start work on writing a
test tool in Python 2.6 which is going to have to work with a few
filehandles, both for read and write actions, as well as socket
connections eventually. So being able to find who has a lock on a
given file or directory if the program dies unexpectedly would be
useful.

Cheers
Jeff



--

Charles de Gaulle - "The better I get to know men, the more I find
myself loving dogs." -
http://www.brainyquote.com/quotes/au...de_gaulle.html
 
Reply With Quote
 
W. eWatson
Guest
Posts: n/a
 
      12-06-2009
J wrote:
> On Sat, Dec 5, 2009 at 20:32, J <(E-Mail Removed)> wrote:
>
>> connections eventually. So being able to find who has a lock on a
>> given file or directory if the program dies unexpectedly would be
>> useful.

>
> Google tells me that the program Process Explorer from SysInternals
> will provide most of the functionality of lsof... so I'll look into
> that and see if it will work. Certainly will help once I start
> opening and writing output files with my python scripts on Windows
> systems...
>
>

What I'm trying to do is really simple. In the Win XP NG, I have two
suggestions to get rid of the Analysis folder and the empty file in it.
One is to use a program like you suggested, and the other is to delete
it from DOS. I just tried cmd prompt, but was not successful. My DOS
skills are long gone, so I have no idea if there is something I
overlooked there. I bored down to Analysis and the into it. DIR showed
an unnamed empty file, so I tried DEL *. I seriously doubt it was removed.

Well, I'm going to reboot sometime later this evening, and knock it out
as I described I was able to do once before after a reboot. Thne I'm
going to fix the Python program and write a file correctly.
 
Reply With Quote
 
W. eWatson
Guest
Posts: n/a
 
      12-06-2009
The original program and code are not mine. I have no idea if that
specific piece of code has ever been tested. Generally the program works
quite well, and when needed creates the Events folder without any
difficulty. That folder is used heavily by writing new data files to it
thousands of times over a month.

I'm trying to do a very simple thing. I go to the Analysis folder, and
try to use Win XP Pro to delete the empty and unnamed file in it. One
just does a right-click on the empty file, and then uses Delete. It
won't let me delete it. If I back up to the folder level, it won't let
me delete the folder, since it's not empty. See the post I made to J
moments ago.
 
Reply With Quote
 
J
Guest
Posts: n/a
 
      12-06-2009
On Sat, Dec 5, 2009 at 21:14, W. eWatson <(E-Mail Removed)> wrote:

> What I'm trying to do is really simple. In the Win XP NG, I have two
> suggestions to get rid of the Analysis folder and the empty file in it. One
> is to use a program like you suggested, and the other is to delete it from
> DOS. I just tried cmd prompt, but was not successful. My DOS skills are long
> gone, so I have no idea if there is something I overlooked there. I bored
> down to Analysis and the into it. DIR showed an unnamed empty file, so I
> tried DEL *. I seriously doubt it was removed.
>
> Well, I'm going to reboot sometime later this evening, and knock it out as I
> described I was able to do once before after a reboot. Thne I'm going to fix
> the Python program and write a file correctly.


And those are your only options, really. From what I've been able to
see, there is no native ability in Linux to actually see who has a
lock on a file that's been opened. And I completely understand your
frustration.

I've not used the process explorer program, so I really can't say how
well it works for this kind of thing. But every post I found
searching for this indicates that it will at least tell you what still
has a lock on the file and maybe then you can figure out what needs to
be closed from there.

Rebooting should always work in cases like this. Rebooting the system
should clear all file locks and is a last resort for a persistent
stale file lock. So yeah, by rebooting, you'll always be able to
release the lock on that file and then delete it once the system is
back and running.

However, that's not something you'd want to do on a production system
except as a last resort. At least, that's not something that I'd want
to do.

So anyway, since you said the code is not yours, does the code
actually close the files anywhere? I'm assuming that it does at some
point, but if not, that really is something that needs to be added in.
As I said in my last post, I am just a novice in the Python stuff,
but I've written enough code in my life to know that you never assume
that a file will be closed properly by the system once the program is
finished running. I'm not saying that YOU specifically are doing
this, but just making the suggestion as this is the kind of problem
that can happen.

Of course, a program that's dying for some reason is a different story...
--

Mike Ditka - "If God had wanted man to play soccer, he wouldn't have
given us arms." -
http://www.brainyquote.com/quotes/au...ike_ditka.html
 
Reply With Quote
 
Tom Pacheco
Guest
Posts: n/a
 
      12-06-2009
W. eWatson wrote:
> I'm trying to store analytic data in a folder called Analysis. If the
> user doesn't have the folder, I make one for him, and then write a txt
> file into it. In this case a histogram of values, x and frequency.
> However, it appears that I made a mistake somewhere and cannot delete
> it using the Win XP delete. Here's the code that determines if
> Analysis exists, and whether to create it.
>
> print "Path for Histogram", self.current_path
> s = self.current_path
> s = "Analysis"
> s = os.path.join("Analysis",s)
> print "s joined ",s <------------- debug print
> if not os.path.exists("Analysis"):
> os.mkdir("Analysis")
> f = file( s, "wb")
> if not f:
> self.LogError( "Histogram file creation error 1" )
> return False
>
> In writing the code to handle all of this, I forgot to write the txt
> folder to the file, nor did I close it. It was someone else's code,
> and I got caught up in the creation of Analysis. Here's what XP tells
> me when I try to delete the folder.
>
> Very funny. I just tried it to get the content of the msg, and XP
> deleted it. Let me recreate it and try again. OK, here it is:
>
> Error Deleting Folder or File.
> Cannot delete Analysis: It is being used by another person or program.
> Close programs that might be using it and try again.
>
> There is no file created, just the folders Analysis\Analysis. One too
> many. The second Analysis shows as an icon for a file of size 0KB.
>
> I printed with the debug print above:
> Path for Histogram Events\v20070206_055012.06.dat
> s joined Analysis\Analysis
> Analysis\Analysis should only be Analysis.


noticed your assigning the same variable twice
-------------------------------
s = self.current_path
s = "Analysis"
-------------------------------
if you are expecting
Analysis\Events\v20070206_055012.06.dat

delete the second line

as others have said if you can not delete the file if must still be open.

why are you talking about deleting a file but only show code for opening it?





 
Reply With Quote
 
W. eWatson
Guest
Posts: n/a
 
      12-06-2009
J wrote:
> On Sat, Dec 5, 2009 at 21:14, W. eWatson <(E-Mail Removed)> wrote:
>
>> What I'm trying to do is really simple. In the Win XP NG, I have two
>> suggestions to get rid of the Analysis folder and the empty file in it. One
>> is to use a program like you suggested, and the other is to delete it from
>> DOS. I just tried cmd prompt, but was not successful. My DOS skills are long
>> gone, so I have no idea if there is something I overlooked there. I bored
>> down to Analysis and the into it. DIR showed an unnamed empty file, so I
>> tried DEL *. I seriously doubt it was removed.
>>
>> Well, I'm going to reboot sometime later this evening, and knock it out as I
>> described I was able to do once before after a reboot. Thne I'm going to fix
>> the Python program and write a file correctly.

>
> And those are your only options, really. From what I've been able to
> see, there is no native ability in Linux to actually see who has a
> lock on a file that's been opened. And I completely understand your
> frustration.
>
> I've not used the process explorer program, so I really can't say how
> well it works for this kind of thing. But every post I found
> searching for this indicates that it will at least tell you what still
> has a lock on the file and maybe then you can figure out what needs to
> be closed from there.
>
> Rebooting should always work in cases like this. Rebooting the system
> should clear all file locks and is a last resort for a persistent
> stale file lock. So yeah, by rebooting, you'll always be able to
> release the lock on that file and then delete it once the system is
> back and running.
>
> However, that's not something you'd want to do on a production system
> except as a last resort. At least, that's not something that I'd want
> to do.
>
> So anyway, since you said the code is not yours, does the code
> actually close the files anywhere? I'm assuming that it does at some
> point, but if not, that really is something that needs to be added in.
> As I said in my last post, I am just a novice in the Python stuff,
> but I've written enough code in my life to know that you never assume
> that a file will be closed properly by the system once the program is
> finished running. I'm not saying that YOU specifically are doing
> this, but just making the suggestion as this is the kind of problem
> that can happen.
>
> Of course, a program that's dying for some reason is a different story...

Well, I'm pretty well done now. I fixed the program so it would write
the file and close it. It did this successfully. I'll worry about
getting the oddball file deleted later.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applet to run on Win 98, Win ME, Win XP, Win Vista & Win 7 ?? Krist Java 6 05-06-2010 11:53 PM
aspnet.mdf file still is created even though i created a custom provider? Sunfire ASP .Net 1 11-10-2007 04:30 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM



Advertisments