0

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))
devblack.exe
  • 428
  • 4
  • 17
  • 1
    You say "it doesn't seem to run" - do you get an error message, does the script hang, does it refuse to even start? Please share any messages or behaviour. – Grismar Nov 18 '21 at 00:23

1 Answers1

0

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))
cosmic_inquiry
  • 2,557
  • 11
  • 23
  • I tried the code above and it worked a treat, but for some reason I can't get the try except pass to work to ignore the files that are in use, I get the following error: [WinError 32] The process cannot access the file because it is being used by another process: – Imraan Mhatey Nov 18 '21 at 21:26
  • See my edited answer ^. Also it's [best practice to not use try: except: pass](https://stackoverflow.com/a/21553825/8927098) – cosmic_inquiry Nov 19 '21 at 01:15
  • Finally got a chance to test it, and it worked perfectly, a bit slow due to the amount of folders it's searching through over the network but it work, last thing, any chance you know how I can add the size of the file to the output. – Imraan Mhatey Nov 23 '21 at 17:11
  • `removed_files.append(file)` -> `removed_files.append(file + " Bytes: " + os.path.getsize(file))` – cosmic_inquiry Nov 23 '21 at 18:09
  • I tried the code to get the file size but it didn't seem to work, removed_files.append(file + " Bytes: " + os.path.getsize(file)) it only gives the normal output and doesn't add the "Bytes: " or os.path.getsize(file) – Imraan Mhatey Dec 01 '21 at 18:58
  • Yeah it probably didn't work because it was after `os.remove` (`os.path.getsize` probably throws an error on a file that isn't there). See my edited answer. – cosmic_inquiry Dec 01 '21 at 19:05
  • Tried it out again, seems like the files aren't being stored in the removed_files = [], probably getting remove before it can get stored there, I added a print(removed_files) at the end of the code and it comes out blank. – Imraan Mhatey Dec 02 '21 at 13:05
  • I would debug by printing out `files` after the first line and making sure that is what you expect. Assuming that it is, `size = os.path.getsize(file)` and `os.remove(file)` will try to assess the file size and remove the file. If either of those throws an error, then it will jump to the except block and I would check the error being printed. If they don't throw an error, then the file and size will be appended to `removed_files`, those are string and int respectively so it wouldn't matter if the file didn't exist at that point. If `removed_files` is empty, then no files were removed. – cosmic_inquiry Dec 02 '21 at 17:53