3

I have a Python program where I'm searching through a directory but for certain sub folders in that directory I want to delete files in them if they were created for more than 7 days and the rest of the files in the directory delete if they were created more than 21 days.

So after the local user are company folders and inside that folders are sub folders that I need such as House, Address, And Phone any suggestion?

import os
from datetime import date,timedelta,datetime
import time
 
#Path for the employee files
path ="C:/inetpub/ftproot/LocalUser/"

#Function to delete the files
def deleteFiles(days,path):
   #Set time Value (86400 secs in 24 hours)
   current_time = time.time()
   time_in_secs = current_time - (days * 86400)
   # Check to see if the path exists
   if os.path.exists(path):
       for root, dirs, files in os.walk(path):
        for file in files:
            full_path = os.path.join(root,file)
            file_status = os.stat(full_path)
            #print( full_path)

            if(current_time - file_status.st_mtime) // (86400) >= days:
                print(full_path)
                #os.remove(full_path)
                #print(full_path + "\nhas been deleted")
   else:
      print("Error Path Doesn't Exist!!!!!!!!!!!!")
        
        
   deleteFiles(7,path)
Sdot2323
  • 59
  • 5

1 Answers1

2

You could first create a list of white_listed folders, and then iterate through the files in them like this:

import os
from datetime import date,timedelta,datetime
import time

path = r"C:/inetpub/ftproot/LocalUser/"
white_list = ["House", "Address"]

lst_white_folder = []
if os.path.exists(path):
    for root, dirs, files in os.walk(path):
        if os.path.split(root)[-1] in white_list:
            # print(os.path.split(root)[-1])
            lst_white_folder.append(root)

# alternatively you could provide the listof folderpaths directly of course
for folder_path in lst_white_folder:
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            print(file)
            #do something
Andreas
  • 8,694
  • 3
  • 14
  • 38
  • Thanks @Andreas I'll test it in the morning – Sdot2323 Oct 14 '20 at 02:06
  • question for you? – Sdot2323 Oct 14 '20 at 13:29
  • @Sdot2323 what do u mean? – Andreas Oct 14 '20 at 14:29
  • I think I figured it out by using the "if root.endswith("House") " @Andrea – Sdot2323 Oct 14 '20 at 14:43
  • That will work, but be aware if you have subfolder with the same name it gets tricky. Example could be New Folder etc. Anyway glad u could solve it. HappyCoding! – Andreas Oct 14 '20 at 18:18
  • No its one way, the first part loops through the folders and gets the path of the relevant folders and puts them into a list. The second part then loops through all folders which are in the previously created list and only there looks for files. Its more complicated than just doing the root.endswith() aproach but propably a bit safer for complex folder structures. – Andreas Oct 14 '20 at 22:20