473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to "tail" a file using file objects ?

There's something I obviously don't understand about Python file objects.
(I'm an experienced programmer, but a python noob)

In the following test code, I open a text file, and seek to EOF.
Then, once/second, I try to read any new lines.
However, the file object sees no increase in file length even when 1000 new 100-char. lines are added(!)
(Same result in python v2.5.2 on Ubuntu 8.04 & v2.6.5 on Scientific Linux 5.4)

What perfectly obvious thing am I missing here?


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python2.6
  2.  
  3. import os
  4. import stat
  5. import time
  6.  
  7. filename = "testfile.txt"
  8. f = open (filename, "r")
  9. f.seek(0, 2)
  10.  
  11. while True:
  12.     line = f.readline()
  13.     size1 = os.stat(filename)[stat.ST_SIZE]
  14.     size2 = os.fstat(f.fileno())[stat.ST_SIZE]
  15.     print "size1 =", size1, "; size2 =", size2, "; line = \"" + line + "\""
  16.     time.sleep(1)
  17.  



---- output ---- (1000 new lines pasted into file after 2nd line)
:
size1 = 10 ; size2 = 10 ; line = ""
size1 = 10 ; size2 = 10 ; line = ""
size1 = 100010 ; size2 = 10 ; line = ""
size1 = 100010 ; size2 = 10 ; line = ""
:
Jan 28 '11 #1
4 2218
Nuts. I always post questions too fast.
This is not a python problem; I get the same results when I recode in C, using file descriptors, so that I'm reasonably low-level.
Apparently, I just never understood Linux file I/O... (for 20 years)...
Sorry to waste everyone's time.
Jan 28 '11 #2
dwblas
626 Expert 512MB
Generally speaking, when you open a file, the significant stats are read/calculated and stored in variables. So EOF is stored in a variable and not changed. In Python, os.stat(fname) returns a tuple. The 6th element is the file's size. You will probably have to poll the file periodically and check for a change in the size.
Jan 28 '11 #3
Markus
6,050 Expert 4TB
Adding to dwblas' post, you could use something like pyinotify to listen for events on files, and then perform actions accordingly.
Jan 28 '11 #4
dwblas, Markus:

Thanks, guys! Between you, you have opened both my eyes!

dwblas: You have confirmed what I was beginning to suspect (but had remained ignorant of all these years).

Markus: I had not even suspected the existence of inotify/pynotify, even though inotify has apparently been in the Linux kernel for some time.

I am a very happy geek right now.
Again, thanks!
Jan 29 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Mel | last post by:
i need to create a unix like "tail" function on my site. the question: the text is displayed in a scrolled area and a new line is added at the end and the entire text is scrolled down so that...
21
by: Owen Zhang | last post by:
What is the best way to implement "tail -f" in C or C++ and higher performance compared to either unix shell command "tail -f" or perl File::Tail ? Any suggestion appreciated. Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.