[python] Writing to a file in a for loop

text_file = open("new.txt", "r")

lines = text_file.readlines()

for line in lines:
        var1, var2 = line.split(",");
        myfile = open('xyz.txt', 'w')
        myfile.writelines(var1)
        myfile.close()
text_file.close()

I have 10 lines of text in new.txt like Adam:8154 George:5234 and so on. Now i want a text file which contains only the names. xyz.txt must contain Adam George and so on. Now the above code leaves me with the 10th name only.

How to have all the 10 names in a single text file.

This question is related to python python-2.7

The answer is


The main problem was that you were opening/closing files repeatedly inside your loop.

Try this approach:

with open('new.txt') as text_file, open('xyz.txt', 'w') as myfile:  
    for line in text_file:
        var1, var2 = line.split(",");
        myfile.write(var1+'\n')

We open both files at once and because we are using with they will be automatically closed when we are done (or an exception occurs). Previously your output file was repeatedly openend inside your loop.

We are also processing the file line-by-line, rather than reading all of it into memory at once (which can be a problem when you deal with really big files).

Note that write() doesn't append a newline ('\n') so you'll have to do that yourself if you need it (I replaced your writelines() with write() as you are writing a single item, not a list of items).

When opening a file for rread, the 'r' is optional since it's the default mode.


It's preferable to use context managers to close the files automatically

with open("new.txt", "r"), open('xyz.txt', 'w') as textfile, myfile:
    for line in textfile:
        var1, var2 = line.split(",");
        myfile.writelines(var1)