Skip to content

Instantly share code, notes, and snippets.

@asalt
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asalt/08ddef08ca476d3e9d0d to your computer and use it in GitHub Desktop.
Save asalt/08ddef08ca476d3e9d0d to your computer and use it in GitHub Desktop.
#check if files in directory aren't in log of previously encountered log
from time import sleep
def filechecker():
print '{} : Checking for new files...'.format(time.ctime())
try : log = open('filelog.log','r+U') #open log file in append mode
except : log = open('filelog.log','ab+') # if it doesn't exist, create it
files = [line.strip('\n') for line in log.readlines()] # list of all files in log
for f in os.listdir(inputdir): # iterate through all files in directory
if re.search(r'(.+\.txt$)',f ) and f not in files: #if file ends in txt and is not in the log
print 'New file found\n{}'.format(f+'\n')
try:
#move file
log.write(f+lineterm)
except Exception, e: print e #print any exception that may come up
log.close() #close log file
while True:
filechecker()
print 'Sleeping...'
sleep(3600) # sleep for an hour
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment