logging - python filter files by modified time -


i have following code:

def filter_by_time(files):     print "---list of log files timecheck: "     f in files:         print f, datetime.datetime.fromtimestamp(os.path.getmtime(f))     print "------"      mins = datetime.timedelta(minutes=int(raw_input("age of log files in minutes? ")))     print "taking ", mins, "minutes"     mins = mins.total_seconds()     current = time.time()     difftime = current - mins     print "current time: ", datetime.datetime.fromtimestamp(current)     print "logs after: ", datetime.datetime.fromtimestamp(difftime)         f in files:             tlog = os.path.getmtime(f)         print "checking ", f, datetime.datetime.fromtimestamp(tlog)         if difftime > tlog:             print "difftime bigger tlog", "removing ", f             files.remove(f)      print "*****list of log files after timecheck"     f in files:         print f, datetime.datetime.fromtimestamp(os.path.getmtime(f))      print "******"       return files 

and sample number of log files. output of above code when enter few minutes is:

list of log files timecheck:   1copy2.log  11:59:40  1copy3.log  12:13:53  1copy.log  11:59:40  1.log  11:59:40  age of log files in minutes? 5  taking  0:05:00 minutes  current time:  2015-07-14 14:02:11.861755  logs after:  2015-07-14 13:57:11.861755  checking  1 copy 2.log 2015-07-14 11:59:40  difftime bigger tlog removing  1copy2.log  checking  1copy.log 2015-07-14 11:59:40  difftime bigger tlog removing  1copy.log  list of log files after timecheck    1copy3.log 2015-07-14 12:13:53  1.log 2015-07-14 11:59:40    collected: 1copy3.log  collected: 1.log 

as can see files has collected not correct. doing checking 4 files see if modified in last 5 mins. removes 2 list should have removed 4 files.

(made edits make easier read)

you removing items list while iterating list. result unpredictable.

try iterating on copy of list so:

for f in files[:]:    # note [:] after "files"     tlog = os.path.getmtime(f)     print "checking ", f, datetime.datetime.fromtimestamp(tlog)     if difftime > tlog:         print "difftime bigger tlog", "removing ", f         files.remove(f) 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -