I wrote the script below which checks the specified path PARENT_DIR
and finds the recently modified directories (+subdirectories) then makes a back up of them into MOVE_DIR
The problem I'm struggling with is that, it seems this script, somehow, doesn't check the mtime of the directories and copies the whole content within the parent directory to the destination address. what am I doing wrong here?
import os
import os.path
import datetime
from shutil import copytree
from shutil import move
from time import time
os.chdir("/home/sina/Desktop/incoming")
def mins_since_mod(fname):
"""Return time from last modification in minutes"""
return (time() - os.path.getmtime(fname)) / 60
PARENT_DIR = '/home/sina/Desktop/incoming'
MOVE_DIR = '/home/sina/Desktop/incoming_New'
# Loop over files in PARENT_DIR
for fname in os.listdir(PARENT_DIR):
# If the file is a directory and was modified in last 10 days
if ((os.path.isdir(fname)) and (mins_since_mod(fname) < 14400)):
copytree(fname, MOVE_DIR) # move it to a new location
if I want to make the question more clear, you can consider the path below with folowing subdirectoris:
/home/sina/Desktop/incoming/A/A1.1
/home/sina/Desktop/incoming/A/A2.2
...
the subdirectories within ../A
such as A2.2
or A1.1
are being modified by another app (this app may add even new subdirectories) and I'd like to have a back up of them every 10 days at this path /home/sina/Desktop/incoming_New/A/{subdirectories}
Any kind of help is appreciated.