How to streamingly read text file and display whenever updated text

G

galeomaga

#!/usr/bin/python
import time
f = open('/home/martin/Downloads/a.txt')
while 1:
for line in f:
print line;
time.sleep(1);
 
G

galeomaga

(e-mail address removed)æ–¼ 2013å¹´10月5日星期六UTC+8下åˆ3時38分51秒寫é“:
#!/usr/bin/python

import time

f = open('/home/martin/Downloads/a.txt')

while 1:

for line in f:

print line;

time.sleep(1);


if __name__ == '__main__':
logfile = open("/home/martin/Downloads/a.txt","r");
while True:
line = logfile.readline();
if not line:
print line;
time.sleep(1);

this also failed
 
J

James Harris

#!/usr/bin/python
import time
f = open('/home/martin/Downloads/a.txt')

Looks like you are on Unix so you can do this from the shell

tail -F /home/martin/Downloads/a.txt

James
 
M

Mark Lawrence

if __name__ == '__main__':
logfile = open("/home/martin/Downloads/a.txt","r");
while True:
line = logfile.readline();
if not line:
print line;
time.sleep(1);

this also failed

Usually please state your OS and Python versions, what you expected to
happen, what actually happened and the full traceback if applicable. In
this case I'd hazard a guess that as you're trying to print something
that evaluates to false you're not likely to see much. You can also
remove the semicolons as they're simply not needed.

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
M

Mark Lawrence

Looks like you are on Unix so you can do this from the shell

tail -F /home/martin/Downloads/a.txt

James

Tail also works on Windows if you've unxutils installed :)

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
N

Nobody

#!/usr/bin/python
import time
f = open('/home/martin/Downloads/a.txt')
while 1:
for line in f:
print line;
time.sleep(1);

So you're trying to implement "tail -f"?

First, check that "tail -f" actually works for your particular use case.

If the process writing the file uses buffered output, data will only
actually be appended to the file when the buffer is full. You can't read
what isn't there.

And if the process creates a new file with the same name, rather than
appending to the existing file, you'll still be reading the old file. You
would need to open the file again to read the new file.
 
M

Mark Lawrence

A bit of googling found me this:
http://www.linux-support.com/cms/implementation-of-tail-in-python/

import time
import sys

def tail_f(file):
interval = 1.0
while True:
where = file.tell()
line = file.readline()
if not line:
time.sleep(interval)
file.seek(where)
else:
yield line

In future could you please quote some context so that it's easier for us
mere mortals to follow the thread, thanks in anticipation.

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
G

galeomaga

Joost Molenaaræ–¼ 2013å¹´10月5日星期六UTC+8下åˆ7時02分05秒寫é“:
A bit of googling found me this:

http://www.linux-support.com/cms/implementation-of-tail-in-python/



import time

import sys



def tail_f(file):

interval = 1.0

while True:

where = file.tell()

line = file.readline()

if not line:

time.sleep(interval)

file.seek(where)

else:

yield line

After tried many times, updated text file is not shown, it only print text at the first time.

#!/usr/bin/python
import time
import sys
import thread

def tail_f(filehandler):
interval = 1.0
while True:
try:
line = filehandler.readline()
where = filehandler.tell()
if not line:
time.sleep(interval)
filehandler.seek(where)
else:
yield line
except:
print "tail_f error"


def readfile(systemname):
try:
filehandler = open("/home/martin/Downloads/a.txt","r");
while 1:
#for line in tail_f(filehandler):
# print line
try:
interval = 1.0
line = filehandler.readline()
where = filehandler.tell()
if not line:
time.sleep(interval)
filehandler.seek(where)
print where
else:
print line
except:
print "tail_f error"
except:
print "Error: readfile"

if __name__ == '__main__':
try:
thread.start_new_thread( readfile, ("Thread-1", ) )
except:
print "Error: unable to start thread"

while 1:
pass
 
C

Chris Angelico

After tried many times, updated text file is not shown, it only print text at the first time.

The implementation of tail has a lot of little oddities to deal with
edge cases. Why not simply use it?

A while ago, I wanted to make a system that would tail a bunch of logs
on a bunch of computers, and display it all to me in a single unified
view. Rather than write something that opened a whole lot of files and
monitored them, I simply forked a 'tail' process for each file and
reacted to its stdout. It was way WAY easier than dealing with
everything that could possibly happen (log rotation, etc, etc, etc) -
not that it'd be impossible to deal with, but it's a waste of time
reinventing this particular wheel. Build on top of what's already
there, save yourself the trouble.

ChrisA
 
S

Steven D'Aprano

if __name__ == '__main__':
try:
thread.start_new_thread( readfile, ("Thread-1", ) )
except:
print "Error: unable to start thread"


Why not? If you can't start a thread, you have a problem with your code.
How do you expect to debug this problem?


"I find it amusing when novice programmers believe their main job is
preventing programs from crashing. More experienced programmers realize
that correct code is great, code that crashes could use improvement, but
incorrect code that doesn’t crash is a horrible nightmare."
-- Chris Smith

http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/
 
G

galeomaga

I can start thread and no exception error print, and I do not know how to use tail in python script
I need to cope with MySQL in python later as all file path stored in it, it is to monitor all text files
 
M

Mark Lawrence

Why not? If you can't start a thread, you have a problem with your code.
How do you expect to debug this problem?


"I find it amusing when novice programmers believe their main job is
preventing programs from crashing. More experienced programmers realize
that correct code is great, code that crashes could use improvement, but
incorrect code that doesn’t crash is a horrible nightmare."
-- Chris Smith

http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/

Roughly translated for the benefit of newbies remove the try/except :)

Also note that a bare except is extremely bad practice, e.g. you can't
stop rogue programs with a CTRL-C

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
C

Chris Angelico

Also note that a bare except is extremely bad practice, e.g. you can't stop
rogue programs with a CTRL-C

Or to be more accurate, a Ctrl-C will cause a jump to your except
clause. Since, in this instance, that's going to emit and die, Ctrl-C
will still stop the program. But yes, catching KeyboardInterrupt
usually isn't your intention.

ChrisA
 
M

Mark Lawrence

Or to be more accurate, a Ctrl-C will cause a jump to your except
clause. Since, in this instance, that's going to emit and die, Ctrl-C
will still stop the program. But yes, catching KeyboardInterrupt
usually isn't your intention.

ChrisA

Good point, but at least this time I typed "rogue" correctly, unlike on
the tutor mailing list :)

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
C

Chris Angelico

<awful>
How do I know that you're not trying to send me to a rouge site?
</awful>

I assure you, the background color is most distinctly white. They
probably contract with Google for their white pixel supply:

http://www.google.com.au/technology/pigeonrank.html

This is, however, quite distinctly off-topic for this list... though
Google does use Python extensively, and until not long ago had GvR on
their payroll.

ChrisA
 
A

Andreas Perstinger


For the readers who don't bother clicking on the link above: It's a
short video where the OP demonstrates how her/his usage of tail doesn't
work.
no matter call tail directly in python or using the script of tail
all failed
it seems it can not read next line

In your video you use gedit to write some file and "tail -f <file>" to
follow it. But "tail -f" will follow the file descriptor. Usually,
editors like gedit won't save your changes to the original file but
create a new temporary file and rename it later to the original file
name after deleting the original one. Thus tail will follow an already
deleted file.
See also this blog post:
http://tech.shantanugoel.com/2009/12/23/continuous-monitor-tail-fails.html

For your example you will have to use "tail -F <file>" which will follow
the file name.

Alternatively you could write a simple script to simulate a continously
growing file like

import time
for i in range(1000):
with open("test.txt", "a") as f:
f.write(str(i) + '\n')
time.sleep(1)

which should work with "tail -f".

Bye, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top