simple bit of code, just prints out a list of urls I put into a text file (it was printing them with the \n char at the end of each one though – how do I get rid of the \n?):
import mechanize, fileinput
with open('F:\Python\url_list2.txt') as urls:
content = urls.readlines()
print content
anyway, it worked printed out the list, great. Run it again and I get this message in the python shell:
<closed file 'F:\Python\url_list2.txt', mode 'r' at 0x0000000002E4E390>
What is going on? Using windows 7 x64 if that makes any difference?
The
withstatement automatically closes the handle after all the statements inside it execute. If you need access to the handle afterwards:If you want to get rid of the
\nat the end of every line, use.strip():