Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using shutil.copytree without copystat

Tags:

python

shutil

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

like image 419
Bok Avatar asked Oct 25 '25 22:10

Bok


1 Answers

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)
like image 100
Nathaniel Avatar answered Oct 28 '25 12:10

Nathaniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!