简体   繁体   中英

Why won't my script write to a file?

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 continue so 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 static 1 . Then you'd see the close call and information printed to the file.

A bare except is almost certainly a bad idea; you should only handle the exception you expect to see. Then if it does something totally unexpected you will still get a useful error trace about it.

import time
import tools
from BeautifulSoup import BeautifulSoup

def scan_file(url, logf):
    try:
        page = tools.download(url)
    except IOError:
        print("Couldn't read url {0}".format(url))
        return

    try:
        soup = BeautifulSoup(page)
        w = soup.find("h1").string
    except AttributeError:
        print("Couldn't find <h1> tag")
        return

    print(w)
    logf.write(w)
    logf.write('\n')

def main():
    with open("randomwords.txt","a") as logf:
        try:
            while True:
                time.sleep(3)
                scan_file("http://wordnik.com/random", logf)
        except KeyboardInterrupt:
            break

if __name__=="__main__":
    main()

Now you can close the program by typing Ctrl-C, and the "with" clause will ensure that the log file is closed properly.

From what i understand, you want to output a random number every three second into a file. But caching will take place, so you will not see your numbers until the cache has grown too large, typically in the order of 4K bytes.

i suggest that in your loop, you add a f.flush() before the sleep() line.

Also, like wheaties sugessted, you should have proper exception handling (if i want to stop your program, i will likely do a SIGINT using Ctrl+C, and your program won't stop in this case) and a proper exit path.

I'm sure that when you test your program, you will kill it hard to stop it, and any random number it has written will not be written because the file is not properly closed. If you program could exit normally, you would have close()d the file, and close() triggers a flush(), and so you would have something written in your file.

Read the answer posted by wheaties.

And, if you want to force to write the file's buffer to the disk, read: http://docs.python.org/library/stdtypes.html#file.flush

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM