Hi All, I'm trying to get the following code to watch a folder for changes and return the filename (preferably a full path) as a string once it's checked that the filesize hasn't increased recently, to stop the rest of my script inspecting incomplete files. I'm having difficulty with sending my filesize timer function a filename because i'm collecting the detected files as a list. If i'm barking up the wrong tree feel free to tell me, and thanks for any help!
Stewart
import os, time
def watch(path_to_watch):
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while watch_active == 1:
time.sleep (10)
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
removed = [f for f in before if not f in after]
if added:
filesizechecker(added)
return added
if removed:
print "Removed: ", ", ".join (removed)
before = after
def filesizechecker(filepath):
# Checks filesize of input file and
# returns 1 when file hasn't changed for 3 seconds
fnow = open(filepath, "rb")
fthen = 1
while fnow != fthen:
time.sleep(3)
fthen = len(f.read())
watch_active = 1
watch("/home/stewart/Documents")