h4r5h4
h4r5h4

Reputation: 443

Writing to a file in a for loop only writes the last value

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. The above code leaves me with the 10th name only.

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

Upvotes: 38

Views: 187033

Answers (3)

John La Rooy
John La Rooy

Reputation: 304147

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.write(var1)

Upvotes: 14

pyfunc
pyfunc

Reputation: 66709

That is because you are opening , writing and closing the file 10 times inside your for loop. Opening a file in w mode erases whatever was in the file previously, so every time you open the file, the contents written to it in previous iterations get erased.

myfile = open('xyz.txt', 'w')
myfile.writelines(var1)
myfile.close()

You should open and close your file outside for loop.

myfile = open('xyz.txt', 'w')
for line in lines:
    var1, var2 = line.split(",");
    myfile.write("%s\n" % var1)
    
myfile.close()
text_file.close()

You should also notice to use write and not writelines.

writelines writes a list of lines to your file.

Also you should check out the answers posted by folks here that uses with statement. That is the elegant way to do file read/write operations in Python

Upvotes: 60

Levon
Levon

Reputation: 143047

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.

Upvotes: 29

Related Questions