[Tutor] file in use oddness with Python 2.3.4 and IDLE 1.0.3 on WinMe

Brian van den Broek bvande at po-box.mcgill.ca
Fri Nov 19 02:04:33 CET 2004


Hi all,

I've got something I cannot work out.

I am using Python 2.3.4 on WinME and IDLE 1.03.

I've previously written (and made much successful use of) the following
utility functions:

def writer(full_file_path, contents):
     '''writes contents to file and closes it when done.

     Given a full file path (or a file name in which case the current
     working directory is assumed) and a list of file contents, writer()
     writelines the contents to the file and closes it.
     '''
     result_file = open(full_file_path, 'w')
     result_file.writelines(contents)
     result_file.close()

def reader(full_file_path):
     '''-> file_contents (as a list)

     Given a full file path (or a file name in the current working
     directory), reader() uses the readlines() method of file objects
     to read the lines into a list, closes the file, and then returns
     the list.
     '''
     the_file_to_read = open(full_file_path, 'r')
     file_contents = the_file_to_read.readlines()
     the_file_to_read.close()
     return file_contents

(Note the calls to file.close() in both functions )

The program I'm currently working on uses the writer() function to write 
a simple log file if a call to os.rename() raises an OSError Exception
(which is expected and caused by the destination filepath of the
os.rename call already existing). Before using writer(), it checks if 
the file exists, and, if it does, uses reader() so as to preserve its 
contents. The only other contact my program has with the file is that if 
it existed before the program was run, it will be part of a list 
returned by a call to os.listdir().

Several times when running my program, and after the program had ended,
the file created by the call to writer() could not be deleted -- I got
the standard Windows "This file is in use by another process" (or
however the standard one is worded) message.

This has happened only when running my program through IDLE, and only
intermittently there. I have tried it in succession with the system
unaltered and all of the data used by my program exactly the same.
Sometimes I get that Windows error message, and sometimes not. Whenever
I do, I need to shut IDLE down before I can delete the writer() created
file. I know that my program exits cleanly, and IDLE sits there happily
waiting for the next thing I want to do.

 From the Python command line and SciTE, I ran the program 10'ish times
each, without the problem manifesting itself. With IDLE, it seems to pop
up ever 3-4 program runs.

I had noticed that some of the times where the Windows error occurred,
the writer() written file was being viewed either by Firefox or by a
text editor. Even though that has never been a problem before, I checked
and was able to replicate when I made certain that there was no other
application viewing the file.

My writer() and reader() functions look good to me, and have never 
caused my troubles before. So, I'm stumped.

Anyone have an idea what might be the trouble? It's not critical for my
program, but questions like this nag at me.

Best to all,

Brian vdB




More information about the Tutor mailing list