How to streamingly read text file and display whenever updated text
#!/usr/bin/python import time f = open('/home/martin/Downloads/a.txt') while 1: for line in f: print line; time.sleep(1); -- https://mail.python.org/mailman/listinfo/python-list
Re: How to streamingly read text file and display whenever updated text
gale...@gmail.com於 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: How to streamingly read text file and display whenever updated text
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: How to streamingly read text file and display whenever updated text
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: How to streamingly read text file and display whenever updated text
https://docs.google.com/file/d/0B2D69u2pweEvelh1T25ra19oZEU/edit?usp=sharing no matter call tail directly in python or using the script of tail all failed it seems it can not read next line -- https://mail.python.org/mailman/listinfo/python-list