gopi1410
gopi1410

Reputation: 6625

Writing in a file in python executed on next run

I have the following code:

f=open('data.txt', 'w')
conn = urllib2.urlopen('http://example.com')
page_html = conn.read()
data=BeautifulSoup(page_html)
count=0
out=""
for val in data.findAll('td'):
    count=count+1
    if(count%2==0 and val.contents):
        out=out+val.contents[0].strip(' \n\t')+"\n"
    if(count>=18):
        f.write(out+"\n")
        break
f.closed

Now when I execute the code, the output of the previous run goes in the file data.txt

For eg, now I have example.com in the url, I run the code then I change the url to stackoverflow.com. Now when I run it again and check data.txt I have the output of example.com in the data.txt file. The next time I run with a different url I am getting output of stackoverflow.com in the file. Can somebody help me out with this? I checked the outputs at every stage in the code. If I directly give the output instead of writing in file, it works perfectly.

Upvotes: 1

Views: 103

Answers (2)

Levon
Levon

Reputation: 143047

You want to close your file with the

 f.close()

function call and thereby flush your output buffers.

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838096

You probably want to use f.close() instead of f.closed. The latter doesn't close the file. It returns a boolean that tells you whether or not the file has been closed.

I'd also suggest that you use a with statement instead of closing the file manually.

with open('data.txt', 'w') as f:
    # etc...

Upvotes: 5

Related Questions