simultaneous reading and writing a textfile

Marco Herrn marco.herrn at sourcepark.de
Tue May 16 17:46:35 CEST 2006


Hi,

I have a text file with some lines in it.
Now I want to iterate over this file and exchange some lines with some
others. I tried this approach:

    try:
        myfile= file('myfile', 'r+')

        while 1:
            line= myfile.readline()
            if not line: break

            l= line.strip().split()
            if len(l) == 1:
                hostname= l[0]
                myfile.write(hostname+' '+mac+'\n')
                break
            
    finally:
        if not myfile is None:
            myfile.close()


This should inspect the file and find the first line, that can't be
split into two parts (that means, which has only one word in it).
This line should be exchanged with a line that contains some more
info.

Unfortunately (or how better python programmers than I am would say,
"of course") this doesn't work. The line is exchanged, but also some
more lines.

Now how can I achieve, what I want? Really exchange one line with
another, regardless of their length. Is this possible? If this is not
possible, then what would be the best approach to do this?

I do not want to read the whole file, exchange the line in memory and
then write the whole file. This would be quite slow with large files.

Regard1s
Marco



More information about the Python-list mailing list