1

I am writing a python program to monitor a file for any modification to the file by an external program. I used the below sample program for monitoring the TEST.txt file but the sample program only work for directory and not for the file. Any help appreciated to make it working for file.

import time
import fcntl
import os
import signal

FNAME = "/HOME/PRASAD/TEST.txt"

def handler(signum, frame):
    print "File %s modified" % (FNAME,)

signal.signal(signal.SIGIO, handler)
fd = os.open(FNAME,  os.O_RDONLY)
fcntl.fcntl(fd, fcntl.F_SETSIG, 0)
fcntl.fcntl(fd, fcntl.F_NOTIFY,
            fcntl.DN_MODIFY | fcntl.DN_CREATE | fcntl.DN_MULTISHOT)

while True:
    time.sleep(10000)
7
  • 1
    github.com/dsoprea/PyInotify Commented Jul 2, 2020 at 20:06
  • Since i have some memory limitation in my target i cannot add any extra python package.I have to use only base python package and that is reason i choose the above sample Commented Jul 2, 2020 at 20:13
  • Well, your strategy does in fact only work on directories and there is no built-in inotify library for python. It may be possible to write one in pure python, but I can't imagine it being more memory efficient than the version written in C that I linked. You could call the inotify command via subprocess, but that's also likely worse on memory Commented Jul 2, 2020 at 20:18
  • Thank you for sharing this information. Commented Jul 2, 2020 at 20:41
  • "it only work" -- what means that exactly? Please take the tour and read How to Ask. Commented Jul 2, 2020 at 20:58

1 Answer 1

1

I would use fcntl to control folder in which is TEST.txt and when something changed in this folder then I would use other methods to check file TEST.txt - ie os.stat(filename).st_mtime or even full os.stat(filename).

I don't know if I need all values DN_ but without DN_ATTRIB it doesn't recognize touch TEST.txt

import time
import fcntl
import os
import signal

DNAME = "/HOME/PRASAD/"
FNAME = "/HOME/PRASAD/TEST.txt"

previous = os.stat(FNAME).st_mtime
#previous = os.stat(FNAME)

def handler(signum, frame):
    global previous
    
    print("DIR: {}".format(DNAME))
    
    current = os.stat(FNAME).st_mtime # <--- file
    #current = os.stat(FNAME)         # <--- file

    if current != previous:
        print("FILE: {} {}".format(FNAME, current))
        previous = current

signal.signal(signal.SIGIO, handler)
fd = os.open(DNAME, os.O_RDONLY)      # <--- directory
fcntl.fcntl(fd, fcntl.F_SETSIG, 0)
fcntl.fcntl(fd, fcntl.F_NOTIFY,
            fcntl.DN_ACCESS |
            fcntl.DN_MODIFY |
            fcntl.DN_CREATE |
            fcntl.DN_DELETE |
            fcntl.DN_RENAME |
            fcntl.DN_ATTRIB |
            fcntl.DN_MULTISHOT)

while True:
    time.sleep(10000)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.