0

I'm working on a python program that should wait for another program to update a .txt file, and when the file was updated my program should open it, get the command the other program just made, and then run something else. I'm quite new to python so a generic but well-explained solution would be great.

I've found How do I watch a file for changes? that leads to the solution here, but I can't quite understand how this works. The program could also be in other languages but it should trigger my python modules.

Here's the general idea of what I'm looking for.

sub GET_FILE_UPDATE:  
try:  
    while change == False:  
       time.sleep(1)  
    else  
       if change == True  
       'open file and read command'  
       run.command()  
    except KeyboardInterrupt:  
       'stop program'
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • [os.path.getmtime](https://docs.python.org/3/library/os.path.html#os.path.getmtime) returns the last modification timestamp. Does a change of that metadata means the file was "modified" (even if the contents did not change)? – Leporello Jun 07 '19 at 14:44
  • The problem is underspecified. You'll need to explain what condition constitutes that this other program is done updating the file in question. That's one stop towards your solution. – IInspectable Jun 07 '19 at 18:50
  • The ideia is that the other program will keep the file open in a blocked mode so that my program can not open it until it updates the file and closes it, so i would have to wait for this to be done so then i would open and get the line the program just wrote in the txt file. – Beatriz Couto de Carvalho Jun 10 '19 at 13:55
  • It can be done, for example, using [FindFirstChangeNotification](https://learn.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstchangenotificationa) API. What you need to understand the API and code sample for Python you have already found. – Rita Han Jun 13 '19 at 08:46

0 Answers0