468,309 Members | 1,075 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,309 developers. It's quick & easy.

Watching a file another app is writing

I'm trying to find a way to take a file that another program has opened
and writes to periodically, open it simultaneously in Python, and
automatically update some of my objects in Python when the file is
written to.
I can open the file and manually readlines() from it to keep up to date,
it's the automatic part I'm having trouble with. This is on Windows.
Mar 11 '07 #1
8 1791
On Mar 11, 3:36 pm, Gordon Airporte <JHoo...@fbi.govwrote:
I'm trying to find a way to take a file that another program has opened
and writes to periodically, open it simultaneously in Python, and
automatically update some of my objects in Python when the file is
written to.
I can open the file and manually readlines() from it to keep up to date,
it's the automatic part I'm having trouble with. This is on Windows.
You might need to look at pywin32 for Windows specific ways to listen
to "file changed" event.

On Unix a quick shortcut would be to simply read the output of 'tail -
f <file>' command...

Mar 12 '07 #2
Nick Vatamaniuc wrote:
You might need to look at pywin32 for Windows specific ways to listen
to "file changed" event.

On Unix a quick shortcut would be to simply read the output of 'tail -
f <file>' command...
Ah, I forgot I have Cygwin installed, so I do have tail. Unfortunately
Windows will not let most programs save to the file while tail is
watching it, and when I force it with vim :w! the output from tail -f
does not update. Maybe I'm using it wrong.
Well, off to dig through windows documentation. Then I have to figure
out how to write and event loop for the shell.
Mar 12 '07 #3
En Mon, 12 Mar 2007 04:25:48 -0300, Gordon Airporte <JH*****@fbi.gov>
escribió:
Nick Vatamaniuc wrote:
You might need to look at pywin32 for Windows specific ways to listen
to "file changed" event.
This article explains it in detail:
http://tgolden.sc.sabren.com/python/...r_changes.html
BTW, it's the top result on Google for "python notify file change windows"

--
Gabriel Genellina

Mar 12 '07 #4
Gordon Airporte schreef:
Nick Vatamaniuc wrote:
You might need to look at pywin32 for Windows specific ways to listen
to "file changed" event.

On Unix a quick shortcut would be to simply read the output of 'tail -
f <file>' command...

Ah, I forgot I have Cygwin installed, so I do have tail. Unfortunately
Windows will not let most programs save to the file while tail is
watching it, and when I force it with vim :w! the output from tail -f
does not update. Maybe I'm using it wrong.
Have you tried with tail -F instead of tail -f? With a big F it uses the
name of the file instead of the file descriptor, and it keeps trying to
open the file. I'm not sure if it will work, but it's worth a shot.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Mar 12 '07 #5
Nick Vatamaniuc wrote:
On Mar 11, 3:36 pm, Gordon Airporte <JHoo...@fbi.govwrote:
>>I'm trying to find a way to take a file that another program has opened
and writes to periodically, open it simultaneously in Python, and
automatically update some of my objects in Python when the file is
written to.
I can open the file and manually readlines() from it to keep up to date,
it's the automatic part I'm having trouble with. This is on Windows.


You might need to look at pywin32 for Windows specific ways to listen
to "file changed" event.

On Unix a quick shortcut would be to simply read the output of 'tail -
f <file>' command...
"tail -f" just checks the file size once a second. It's not doing
anything exciting.

Windows, of course, actually has a mechanism for finding out that
files have changed, so that will work.

Note that if you're doing this as a means of interprocess
communication, there are better ways.

John Nagle
Mar 12 '07 #6
Gordon Airporte wrote:
I'm trying to find a way to take a file that another program has opened
and writes to periodically, open it simultaneously in Python, and
automatically update some of my objects in Python when the file is
written to.
I can open the file and manually readlines() from it to keep up to date,
it's the automatic part I'm having trouble with. This is on Windows.
It occurs to me under Unix you could perhaps get your first program to write
to a "named pipe", which you 2nd program could read from. See

http://en.wikipedia.org/wiki/Named_pipe

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Mar 12 '07 #7
In article <js*****************@newssvr25.news.prodigy.net> , John Nagle wrote:
>On Unix a quick shortcut would be to simply read the output of 'tail -
f <file>' command...

"tail -f" just checks the file size once a second. It's not doing
anything exciting.
That's not actually always true these days. *BSD, at least, use
'kqueue' to avoid busy-wait on "tail -f".
Mar 12 '07 #8
Gabriel Genellina wrote:
This article explains it in detail:
http://tgolden.sc.sabren.com/python/...r_changes.html

BTW, it's the top result on Google for "python notify file change windows"

--Gabriel Genellina
Ah, excelent. Thank you. I'd started with the pywin32 documentation,
where I did manage to find FindFirstChangeNotification(). I had to go to
bed before I could get to Google to figure out how to use it.
Having just tested it, it seems that my problem is now caching. These
only trigger when the file is written to disk, and for the small amounts
of data I'm dealing with that's not with every transaction.
This is probably why tail wasn't working for me either, with the added
complication of it locking the file.
Mar 12 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Christopher Harrison | last post: by
6 posts views Thread by Ajay Pal Singh | last post: by
15 posts views Thread by xkenneth | last post: by
11 posts views Thread by jcor | last post: by
14 posts views Thread by Zed A. Shaw | last post: by
5 posts views Thread by Andrew Cooper | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.