On 9/2/2012 6:36 AM, Gilles wrote:
On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <nos...@nospam.com> wrote:
(snip)

Found it:

#rewrite lines to new file
output = open('output.txt','w')

for line in textlines:
        #edit each line
        line = "just a test"
        output.write("%s" % line)

output.close()

If you process each line separately, there is no reason to read them all at once. Use the file as an iterator directly. Since line is already a string, there is no reason to copy it into a new string. Combining these two changes with Mark's suggestion to use with and we have the following simple code:

with open('input.txt', 'r') as inp, open('output.txt', 'w') as out:
    for line in inp:
        out.write(process(line))

where for your example, process(line) == 'just a test\n'
(you need explicit line ending for .write())

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to