You can use a python package called Watchdog.
This example shows monitoring the current directory recursively for file system changes, and logging any to the console:
import time
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path='.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
You could use this in conjunction with Ignacio's answer - use file_pointer.tell()
to get the current position in the file, and then seek()
there next time, and read the remainder of the file. For example:
# First time
with open('current.csv', 'r') as f:
data = f.readlines()
last_pos = f.tell()
# Second time
with open('current.csv', 'r') as f:
f.seek(last_pos)
new_data = f.readlines()
last_pos = f.tell()