Search code examples
pythonglobos.walk

Walk directories and remove file extensions


I'm trying to remove all the outlook .ost and .nst files from the user's folder on a network PC, as well as I'm trying to get it to write what files were removed into a CSV file.

I'm able to get it to find all the files in the directory and write it to a CSV file but when I try to remove the files with os.remove it doesn't seem to run, I hashed it out for the time being.

I added in the try and except, to skip the files that are in use.

import os
import sys

sys.stdout = open("output_file.csv", "w")
try:
    for rootDir, subdir, files in os.walk("//network_pc_name/c$/Users"):
        for filenames in files:
            if filenames.endswith((".nst",".ost")):
                foundfiles = os.path.join(rootDir, filenames)
                #os.remove(os.path.join(rootDir, filenames))
                print(foundfiles)
except:
    pass
sys.stdout.close()

I made some change to the script as suggested and it appears to run alot quicker, however, I can't seem to figure out how to ignore files which are in use.

I switched the files extensions to .xlsx and .txt files to simulate the .xlsx file being open receiving the permissions error and to see if the script would continue to run and remove the .txt file.

I got the following error: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '//DESKTOP-HRLS19N/c$/globtest\Book1.xlsx

import glob
import os

files = [i for i in glob.glob("//DESKTOP-HRLS19N/c$/globtest/**", recursive = True) if i.endswith((".xlsx",".txt"))]

[os.remove(f) for f in files]
with open("output_file.csv", "w") as f:
    f.writelines("\n".join(files))

Solution

  • In my experience glob is much easier:

    print([i for i in glob.glob("//network_pc_name/c$/Users/**", recursive=True) if i.endswith((".nst", ".ost"))])
    

    Assuming that prints out the files you're expecting:

    files = [i for i in glob.glob("//network_pc_name/c$/Users/**", recursive=True) if i.endswith((".nst", ".ost"))]
    removed_files = []
    for file in files:
        try:
            size = os.path.getsize(file)
            os.remove(file)
            removed_files.append(file + " Bytes: " + size)
        except Exception as e:
            print("Could not remove file: " + file)
    with open("output_file.csv", "w") as f:
        f.writelines("\n".join(removed_files))