Social Coder
Social Coder

Reputation: 103

How to write more than one line to a file sequentially in Python

Please see the code below -

from sys import argv
from urllib2 import urlopen
from os.path import exists

script, to_file = argv

url = "http://numbersapi.com/random"

fact = 0
number = 0

print "Top 5 Facts of The World"

while fact < 5:
    response = urlopen(url)
    data = response.read()
    fact += 1
    number += 1
    print
    print "%s). %s " % (str(number), data)

print "Now, let us save the facts to a file for future use."
print "Does the output file exist? %r" % exists(to_file)
print "When you are ready, simply hit ENTER"
raw_input()
out_file = open(to_file, 'w')
out_file.write(data)
print "Alright, facts are saved in the repo."
out_file.close()

The problem in the above code is when I open the file1.txt, I see only 1 fact printed. As a variation, I brought everything inside the while loop. It leads to the same problem. I believe it writes one fact, but then overwrites with the next and next, until only last fact is saved.

What am I doing wrong?

Upvotes: 2

Views: 211

Answers (4)

Mailerdaimon
Mailerdaimon

Reputation: 6080

You overwrite data with every loop iteration. Try this:

out_file = open(to_file, 'w')
while fact < 5:
    response = urlopen(url)
    data = response.read()
    fact += 1
    number += 1
    print
    print "%s). %s " % (str(number), data)
    out_file.write(data)
    out_file.write('\n') #one fact per line

out_file.close()

Upvotes: 2

gizzmole
gizzmole

Reputation: 1627

"data" holds only the last value assigned to it.

from sys import argv

script, to_file = argv

fact = 0
number = 0

out_file = open(to_file, 'w')
while fact < 5:
    data = str(fact)
    out_file.write(str(data) + '\n')
    fact += 1
    number += 1
    print
    print "%s). %s " % (str(number), data)
out_file.close()

Upvotes: 3

Maciej Gol
Maciej Gol

Reputation: 15854

The issue is that you are writing to file after the loop, so that data points at the last url data fetched. To fix this, store data in a list, and then write everything from the list like so:

for fact in data:
    out_file.write(fact + '\n')

You'll need to append the fact fetched like so:

data.append(response.read())

Or ask if you want to write it to the file before fetching facts, and then move file operations like so:

with open(to_file, 'wb') as out_file:
    while fact < 5:
        response = urlopen(url)
        data = response.read()
        if should_write:
            out_file.write(data + '\n')
        fact += 1
        number += 1
        print
        print "%s). %s " % (str(number), data)

Upvotes: 0

dmoreno
dmoreno

Reputation: 706

It seems you are overwritting over data on the loop, so at the end you have only the last data. Try changing to something like this:

[...]
final_data=''
while fact < 5:
    response = urlopen(url)
    data = response.read()
    fact += 1
    number += 1
    print
    print "%s). %s " % (str(number), data)
    final_data+=data

[...]
out_file.write(final_data)

Upvotes: 0

Related Questions