[Tutor] Editing files through Python

Jeff Shannon jeff at ccvcorp.com
Wed Sep 1 21:29:42 CEST 2004


Bryan wrote:

> Hi Bob,
>  
> I've been playing around with the advice you was sent me and have become 
> a little confused about the ideas of the input_file (r) and the 
> output_file (w).  Can they be the same file?  Do I have to close the 
> input file before writing to the output file?  Does this example show 
> what always has to be done when using input and output files?
>  
> Here is the program I wrote following your example, and an error that 
> always appears, relating to the input file, that is perplexing me.  I 
> think it probably has a simple solution, but I just don't understand 
> enough about input and output files to figure it out.  Is there anything 
> in the Python tutorial or elsewhere that I can read more about reading 
> and writing to input and output files.

First, read the error message carefully.

No such file or directory: C:\\BryFolder\\kt.1.24.data.036rd.test.dat

This error is coming from the operating system underneath Python, and 
is saying that it can't find that file.  Are you sure that this file 
already exists?  You will have a difficult time reading from a file 
you haven't created yet. ;)


Now, to answer your actual question -- you (generally) can't have more 
than one active filehandle to a given file at once.  This means that, 
if you open the file for reading, then you need to close it before you 
reopen it for writing.

> input_file = file('C:\BryFolder\coherence.dat', 'r')
> lines = input_file.readlines()
# add following line:
   input_file.close()
> output_file = file('C:\BryFolder\coherence.dat', 'w')
> for line in lines:
>    nums = line.split(" ") 
>    retain = nums[:5] 
>    new_line = ", ".join(retain)+"\n" 
>    output_file.write(new_line)  
> output_file.close()

Keep in mind that the first thing that happens when you open the file 
for writing is that, if it already exists, the contents are discarded. 
    This means that, if something does go wrong, you could lose all 
data in the file.  If you have copies of the data elsewhere, this may 
not matter, but it might be a bit safer to write to a different file, 
just in case.  (Writing to a different file would also make it a bit 
clearer whether a given file had already been processed or not...)

There *is* also a read/write file mode, where you can have a single 
file object that can be both read from and written to.  This can be a 
bit tricky, though -- writing is always "replace" rather than 
"insert", and it's usually necessary to seek() a particular location 
before writing and flush() after writing, even if you think that 
you're already at the right location in the file.  (There's some "fun" 
side-effects of the underlying C library's file-buffering scheme...) 
And in your case, it would have little benefit anyhow -- you don't 
want to leave part of the file as it is, so you might as well let 
open() truncate the file to nothing for you instead of doing it yourself.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list