Watching a file another app is writing

Discussion in 'Python' started by Gordon Airporte, Mar 11, 2007.

  1. 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.
     
    Gordon Airporte, Mar 11, 2007
    #1
    1. Advertising

  2. On Mar 11, 3:36 pm, 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.


    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...
     
    Nick Vatamaniuc, Mar 12, 2007
    #2
    1. Advertising

  3. 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.
     
    Gordon Airporte, Mar 12, 2007
    #3
  4. Gabriel Genellina, Mar 12, 2007
    #4
  5. 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
     
    Roel Schroeven, Mar 12, 2007
    #5
  6. Gordon Airporte

    John Nagle Guest

    Nick Vatamaniuc wrote:
    > On Mar 11, 3:36 pm, 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.

    >
    >
    > 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
     
    John Nagle, Mar 12, 2007
    #6
  7. 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/
     
    Jeremy Sanders, Mar 12, 2007
    #7
  8. Gordon Airporte

    Jon Ribbens Guest

    In article <jsgJh.9412$>, 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".
     
    Jon Ribbens, Mar 12, 2007
    #8
  9. Gabriel Genellina wrote:
    > 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.
     
    Gordon Airporte, Mar 12, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,414
    Scott Allen
    Jun 4, 2005
  2. Bomb Diggy
    Replies:
    11
    Views:
    6,171
    Ivan S Kirkpatrick
    Jul 3, 2003
  3. aljosa

    file parsing/watching + qt

    aljosa, May 4, 2005, in forum: Python
    Replies:
    2
    Views:
    331
    Swaroop C H
    May 4, 2005
  4. Benjamin D. LeMasurier

    watching a file

    Benjamin D. LeMasurier, Oct 1, 2004, in forum: C Programming
    Replies:
    5
    Views:
    304
    dandelion
    Oct 4, 2004
  5. Christopher Harrison

    Watching a File for Changes

    Christopher Harrison, Oct 18, 2005, in forum: C Programming
    Replies:
    8
    Views:
    328
    Netocrat
    Oct 19, 2005
Loading...

Share This Page