38
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?

3 Answers 3

60

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

Sign up to request clarification or add additional context in comments.

8 Comments

Or use with before the loop.
it didn't make any change to the result
@user1481274: I think you are using writelines
Won't you need the '\n' with your myfile.write(var1) statement?
@Levon: You are correct. I was not focusing on how but what he got wrong in his solution. file opening inside loop and incorrect usage of writeline. In fact usage of with is more elegant but that is another jump in understanding and did mention since solution was already provided for that :)
|
29

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.

Comments

14

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)

3 Comments

I knew you could open multiple files with with (I do the same above) but I didn't know this format with the file variables at the end .. learned something new, neat.
shouldn't be indeented for under with?
@andilabs, Thanks. Sure should. Better late than never :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.