Wayne Watson wrote:
Enclosed is a segment of a program which copies a txt file, but replaces the first line with a new one. The new one has a suffix of tmp. After it executed the code and exited the program normally (running out of code), I couldn't access the new tmp file. It was listed by Win XP, but I got a "file in use" messge. Do I have to exit differently to free the tmp file for inspection?

I don't have the answer - but a suggestion for cleaner code. Note I added \n at the end of the header!

def modify_txt_file(old_fname, new_event_date):
    # Open old txt file and change first line with date & time of event
    # renaming will occur a little later
    #    old_fname is complete file name
    #    new_event_date is date like: Tue 2008/03/... 14: ...
    old_prefix = old_fname[0:19]
    input_file=open(old_fname,'r')
    #copy to temporary
    output_file=open(old_prefix+'.tmp','w')
           input_file.readline() # discard first line
           output_file.write("Event time: " + new_event_date + "\n") # replace header
           output_file.write(input_file.read()) # copy rest of file - assuming file is not BIG
    output_file.close()
    input_file.close()
    print "modified txt file with event info"
    # now copy tmp back to ...
    return



-- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to