简体   繁体   中英

Python - Not reading from file?

I'm playing with the generator for processing infinite data, located here , which is a pretty simple thing for returning the tail from a file:

(Source code from here )

# follow.py
#
# Follow a file like tail -f.

import time
def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

# Example use
# Note : This example requires the use of an apache log simulator.
# 
# Go to the directory run/foo and run the program 'logsim.py' from
# that directory.   Run this program as a background process and
# leave it running in a separate window.  We'll write program
# that read the output file being generated
# 

if __name__ == '__main__':
    logfile = open("run/foo/access-log","r")
    loglines = follow(logfile)
    for line in loglines:
        print line,

My issue is that using a test file, '/var/log/test.log' when I write to it, it doesn't print anything to the console I have my generator working on.

I can print the file from other terminals and see that the new lines are added, but the python code doesn't seem to read the new data?

I thought that maybe it was an issue reading from /var/log/ so I created another file in /Users/Me/Python/ and it still won't read the data.

I feel like I'm missing something really simple :(

Edit:

(I'm on OSX, Python 2.7.5 btw) I tried it out line by line, like so:

FID = open(fileName, 'r')
FID.seek(0,2)
FID.tell() # For example says 125
FID.readlines() # returns []
# Now I edit the log file, add in a couple of new lines (in TextMate if that helps)
FID.readlines() # returns []
FID.seek(0,2)
FID.tell() # returns 125
os.stat(fileName) # says size is 142

Is my file descriptor not reading the updated file or something?

Got the same problem on OSX Yosemite with Brew's Python 2.7.8

Fixed it by adding the following just before trying to read freshly added data: FID.seek(0, os.SEEK_CUR)

This does not really change the filepointer, but somehow invalidates some cached state and sees new data.

Let me know if this works for you as well.

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