3
Contributors
3
Replies
29
Views
2 Years
Discussion Span
Last Post by Niloofar24
1

This is how you post to a text file

f = open('url.txt', 'w') #Opens the file to write text

f.write(url) #Writes the file to the opened text file
2

As Andrae posted,but need to close file object.

f = open('url.txt', 'w') #Opens the file to write text
f.write(url) #Writes the file to the opened text file
f.close()

Or the best approch is to use with open(),no need to close file object.

import urllib

url = 'http://www.python.org'
text = urllib.urlopen(url).read()
with open('url.txt', 'w') as f:
    f.write(text)

Edited by snippsat

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.