Thursday, 15 January 2015

Python: Copy files from a folder to another that created 2 days ago -



Python: Copy files from a folder to another that created 2 days ago -

i want re-create files have been created 2 days ago folder other.

this code:

src = '/home/user' dst = '/var/tmp/backup_tmp' two_days = datetime.now() - timedelta(days=2) filetime = datetime.fromtimestamp(path.getctime(src)) file in os.listdir(src): if file not in os.listdir(dst) , os.path.isfile(file): if filetime <= two_days: print "file more 2 days old" else: shutil.copy(os.path.join(src, file), dst)

i not have errors when run script files not re-create destination folder.

can help me find wrong this???

brs, spyros

you calculating filetime outside if loop. seems problem. try:

for f in os.listdir(src): filetime = datetime.fromtimestamp(path.getctime(os.path.join(src,f))) if f not in os.listdir(dst) , os.path.isfile(f): if filetime <= two_days: print "file more 2 days old" else: shutil.copy(os.path.join(src, f), dst)

python file copy compare

No comments:

Post a Comment