Hi Everyone, I am new to Python and jumping into automating certain processes. Learning so much.
One of the scripts I am working on is figuring out how to remove folders/files from a network drive older then 2 days. Then having one sub set of folders remain for at least 14 days before removing them. I am able to go into the main folder and remove any file or folder older then 2 days. However trying to figure out the best way still for telling the script not to touch folderB until it is older the 14 days inside of testDirectory but remove folderA only every 2 days in testDirectory. I was thinking of using if else to further it but seem to be stuck here if that is the best route. Also wondering if I should do an overall for loop to go through every folder inside of testDirectory. Got this far and not giving up but curious if this script looks ok so far for the 2 days removal.
import os import time import shutil two_days = 2 path =r"E:\testDirectory" now = time.time() for testFolder in os.listdir(path): if os.path.getmtime(os.path.join(path, testFolder)) < now - two_days * 86400: try: print(testFolder) for root, dirs, files in os.walk(os.path.join(path, testFolder), topdown=False): for fileName in files: os.chmod(os.path.join(root, fileName), 0o777) os.remove(os.path.join(root, fileName)) for dirName in dirs: os.rmdir(os.path.join(root, dirName)) os.rmdir(os.path.join(path, testFolder)) except Exception as e: print ("error removing file/directory") print (e)