[Tutor] in as a stream, change stuff, out as a stream?

D-Man dsh8290@rit.edu
Wed, 11 Jul 2001 23:33:28 -0400


On Wed, Jul 11, 2001 at 05:42:16PM -0700, Israel Evans wrote:
| I know I should be able to open up a file, read all of it's contents into a
| list with readlines() or read one line at a time with readline(), and then
| write all of that out to another file.  I think that since the files are
| rather large, it might be best to avoid the speed of readlines() and go with
| readline() repeatedly.

Use xreadlines -- it gives the convenience of readlines without the
memory overhead.

| At any rate, I was wondering if it would be possible to read a line, change
| it in the same file I'm reading and move on to the next line, when I'm done.
| Is this possible?  Or should I read everything at once, change the name of
| the old file and write everything out to a file with the same name as the
| old one.  

It is possible to open a file in "rw" mode (read and write) (or maybe
"ra" so that it doesn't get truncated -- read the docs and test first)
and use random access to move the file pointer to where you want to
write text and write it.  The problem here is if the data you want to
write is not the _exact_ same size as the existing data you will have
problems -- either some of the old data will trail the new data
(because it was never removed) or the new data will overwrite some of
the existing data that wasn't supposed to be changed.

| Is it possible to open a file as a stream  and output it back into itself?
| or is that just plain goofy.

You can try it out with a test file.  Create some text in a file, then
open it in write mode, then try to read it.  When you open the file in
write mode it immediately truncates the file to have a length of zero.
This is why temporary files are commonly used.



Read the existing data in line-by-line (with xreadlines), make the
change to the line you want to change, and output it (line-by-line) to
a temporary file.  Then overwrite the original with the old file (use
os.rename) and you won't have extra copies lying around on your disk.

-D