Home > OS >  How to delete files from multiple paths in Python?
How to delete files from multiple paths in Python?

Time:04-08

I was following a tutorial on creating a Python script for deleting files that are older than a certain amount of days. I was able to successfully create the script and have it do what I want but now I need it to scan multiple paths not just the one. How would I go about doing that?

This is what I have:

import os
import datetime
import glob

path = r'C:\Users\user\Desktop\TEST FILES TO DELETE'
logging_path = r'C:\Users\user\Desktop\Delete Logs'

# Create log directory, wills skip if exists
if not os.path.isdir(logging_path):
    os.mkdir(logging_path)
else:
    print ("Directory already exists")

today = datetime.datetime.today() # Get current time
os.chdir(path) # Changing path to current path

# Create log file with timestamp
file=open(logging_path datetime.datetime.today().strftime('%d-%m-%Y') '.txt','a')

for root, directories, files in os.walk(path, topdown=False):
    for name in files:
        # Check last modified time
        t = os.stat(os.path.join(root, name))[8]
        filetime = datetime.datetime.fromtimestamp(t) - today

        # Is file older than 3 days?, If yes, delete.
        if filetime.days <= -3:
            print(os.path.join(root, name), filetime.days)
            file.write(os.path.join(root, name) ' created ' str(-1*filetime.days) ' days ago\n')
            os.remove(os.path.join(root, name))

This is what I tried doing:

import os
import datetime
import glob

path = [r'C:\Users\user\Desktop\TEST FILES TO DELETE', r'C:\Users\john.ball\Desktop\TEST FILES TO DELETE3']
logging_path = r'C:\Users\user\Desktop\Delete Logs'

# Create log directory, wills skip if exists
if not os.path.isdir(logging_path):
    os.mkdir(logging_path)
else:
    print ("Directory already exists")

today = datetime.datetime.today() # Get current time
os.chdir(path) # Changing path to current path

# Create log file with timestamp
file=open(logging_path datetime.datetime.today().strftime('%d-%m-%Y') '.txt','a')

for root, directories, files in os.walk(path, topdown=False):
    for name in files:
        # Check last modified time
        t = os.stat(os.path.join(root, name))[8]
        filetime = datetime.datetime.fromtimestamp(t) - today

        # Is file older than 3 days?, If yes, delete.
        if filetime.days <= -3:
            print(os.path.join(root, name), filetime.days)
            file.write(os.path.join(root, name) ' created ' str(-1*filetime.days) ' days ago\n')
            os.remove(os.path.join(root, name))

After trying this though I get an error about chdir on line 15. I'm clearly missing something but I don't know enough about Python or programming to figure that out yet.

CodePudding user response:

As Giacomo Catenazzi alredy explained, you changed the data type of path (from a string to a list, wich is incompatible whith chdir). So you need to go through each member of your list like this:

for p in path:
    os.chdir(p)
    # the rest of your code

Note that you need to update every path to p.

CodePudding user response:

Your path variable is an array of paths and accordingly rename it to paths, you need to either index the array in your os.chdir and your os.walk or change the path variable to be a string of one specific path.

  • Related