import time
import traceback
import sys
import tools
from BeautifulSoup import BeautifulSoup
f = open("randomwords.txt","w")
while 1:
try:
page = tools.download("http://wordnik.com/random")
soup = BeautifulSoup(page)
si = soup.find("h1")
w = si.string
print w
f.write(w)
f.write("\n")
time.sleep(3)
except:
traceback.print_exc()
continue
f.close()
It prints just fine. It just won’t write to the file. It’s 0 bytes.
You can never leave the while loop, hence the
f.close()call will never be called and the stream buffer to the file will never be flushed.Let me explain a little bit further, in your exception catch statement you’ve included
continueso there’s no “exit” to the loop condition. Perhaps you should add some sort of indicator that you’ve reached the end of the page instead of a static1. Then you’d see theclosecall and information printed to the file.