4

I am trying to write a python script backing up a folder, and keeping it for x days.

I use

shutil.copytree(source, finaldest)

My problem is, the timestamp from the original files persists, meaning the folders will be deleted if the files within is older than x days. What i want is the timestamp to be the time of backup, regardless of the original creation date

Bok
  • 537
  • 5
  • 21
  • Do you need to access the contents of the folder? You could instead create an archive that way the files are preserved, but you still get the modified time. – Nathaniel Sep 22 '15 at 15:00
  • This is not optimal for me. The script will backup receipts (this is for a construction company, meaning there is alot of them) in case someone accidentally deletes an important one. I setup a special cloud login to access the backups, and thus would like to reproduce the original structure for ease of use – Bok Sep 22 '15 at 15:06

1 Answers1

3

After doing the copytree(), you can then modify the timestamps on the files like so:

import os

for dirpath, _, filenames in os.walk(finaldest):
    os.utime(dirpath, None)
    for file in filenames:
        os.utime(os.path.join(dirpath, file), None)
Nathaniel
  • 770
  • 7
  • 14
  • I get the following error: TypeError: utime() takes exactly 2 arguments (1 given) – Bok Sep 22 '15 at 15:33
  • Apparently `utime()` needs a `None` parameter to specify that it should use the current time, or you could specify your own custom time as a tuple (see the docs for more info). – Nathaniel Sep 22 '15 at 15:36
  • Thanks for helping out! your solution works flawlessly – Bok Sep 22 '15 at 15:41