python program to watch a file - example.

Tim Hoffman timh at zute.net
Sun Sep 8 09:53:20 EDT 2002


Wayne R wrote:
> Sorry for the html,  I forgot to turn it off (as I usually turn it off
> as soon as I install a new system, forgot this time).
> 
> The platform is Gentoo Linux,  I am writing a piece of code for the
> Twiggi project to patch in adduser support.
> 
> 
> |-> > >I am trying to write a python program that will run as a deamon
> and
> |-> > >watch a file for data to arrive and then wake up and perform
> duties
> |-> on
> |-> > >that data.   I found ways to make a python program run as a
> deamon
> |-> but I
> |-> > >am at a loss to find anything related to watching a file for data
> so
> |-> > >that it can then process the data.  I didn't really want to do a
> |-> sleep
> |-> > >loop or something like, I am sure there is a better way.
> |-> > >
> |-> > >Any ideas or links to more info?
> |-> > >
> |-> > Don't annoy people you want help from, for starters.
> |-> > What system are you targeting? (Outlook in the headers says you're
> on
> |-> windows
> |-> > generating this abominable mix of actual message and useless
> cruft).
> |-> >
> [Wayne R] 
> 
> 
> 

Are you talking about the same file always (ie you don't have a new 
file, overwriting the old one ?).  Anyway you really need to look at
how the underlyng OS, can handle such an event.

If it is always the same file and seeing you are looking at *nix 
varient, one of the unix like ways would be to tail -f the file, and 
hook it up to your process which just does blocking reads on stdin.

ie tail -f somfile | python my_watch.py

Rgds

Tim




More information about the Python-list mailing list