Watching a file another app is writing

G

Gordon Airporte

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.
 
N

Nick Vatamaniuc

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...
 
G

Gordon Airporte

Nick said:
> 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.
 
R

Roel Schroeven

Gordon Airporte schreef:
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.
 
J

John Nagle

Nick said:
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
 
J

Jeremy Sanders

Gordon said:
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
 
J

Jon Ribbens

"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".
 
G

Gordon Airporte

Gabriel said:
> This article explains it in detail:
http://tgolden.sc.sabren.com/python/win32_how_do_i/watch_directory_for_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.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top