Reputation: 517
I'm writing a backup script that runs through a folder structure and copies any files that have been changed since the last run through. I can detect modifications and creations through the files' properties (using getmtime
and getctime
) but I also need to be able to detect if a file has been moved. Is there a simple way of doing this without having to record the whole file structure and comparing on each update?
Note that this will only be used on a Windows system.
EDIT: If possible I'd like to avoid using external libraries.
Upvotes: 2
Views: 1758
Reputation: 1323
You can run a daemon that monitors the parent folder for any changes, by using, E.G., the watchdog library:
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
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()
Upvotes: 3