python - Comparing file creation date -
i trying archive old files based on creation date. have data starting 12-17-2010 setting base date , incrementing there. here code
import os, time, tarfile datetime import datetime, date, timedelta import datetime path = "/home/appins/.scripts/test/" count = 0 set_date = '2010-12-17' date = datetime.datetime.strptime(set_date, '%y-%m-%d') while (count < 2): date += datetime.timedelta(days=1) tar_file = "nas_archive_"+date.strftime('%m-%d-%y')+".tgz" log_file = "archive_log_"+date.strftime('%m-%d-%y') fcount = 0 f = open(log_file,'ab+') #print date.strftime('%m-%d-%y') root, subfolders, files in os.walk(path): file in files: file = os.path.join(root,file) file = os.path.join(path, file) filecreation = os.path.getctime(file) print datetime.fromtimestamp(filecreation)," file creation date" print date.strftime('%m-%d-%y')," base date" if filecreation == date: tar.add(file) f.write(file + '\n') print file," of matching date" fcount = fcount + 1 f.close() count += 1
filecreation variable getting float value. how can use compare base date?
timestamp = datetime.mktime(date.timetuple())
the 'timestamp' contain timestamp comparable values returned getctime. regarding comment under question: on windows getctime returns creation time, on unixes modification time (http://docs.python.org/3.1/library/os.path.html).
edit (regarding questions in comment):
1) mktime present in python 2.x: http://docs.python.org/2/library/time.html#time.mktime
2) get file creation time python on linux
edit2:
obviously stupid, , 1 should proceed suggested tdelaney below:
date.fromtimestamp(filecreation)
and compare dates, not timestamps. wasn't looking @ algorithm doing :)
Comments
Post a Comment