-1

I am trying to create a python script to help clean up a folder that I create with powershell. This is a directory that contains folders named after the users for them to put stuff into. Once they leave our site, their folder remains and for all new staff who come a folder gets created. This means that we have over 250 folders but only 100 active staff. I have a test folder that I am using so that I can get the script working and then add in extra bits like moving the directories to an old location, and then deleting them based on age. But at the moment I am working on the delete portion of the script. So far I have the below script, it runs with no errors, but it doesn't actually do anything and I am failing to see why.. It should be reading a csv file that I have of all current staff members, and then comparing that to the folders located in the filepath and then removing any folders that dont match the names in the CSV file. The CSV file is generated from powershell using the same script that I used to create them.

import os
import pandas as pd

path = "//servername/Vault/Users$"


flist = pd.read_csv('D:/Staff List.csv')

file_name = flist['fileName'].tolist()

for fileName in os.listdir(path):


#If file is not present in list
if fileName not in file_name:
    #Get full path of file and remove it
    full_file_path = os.path.join(path, fileName)
    os.remove(full_file_path)
Malenko
  • 1
  • 1
  • 2
    So, what is your question? You've described the situation you're in well, and shown some code, but you haven't said anything about what's wrong with it. Are you getting an error? If so, please [edit] your question to include the full traceback. Is it removing the wrong folders? Give us an example that's not dependent on your file system and the contents of your CSV file. – Blckknght Mar 25 '21 at 00:27
  • 2
    Does this answer your question? [How to delete the contents of a folder?](https://stackoverflow.com/questions/185936/how-to-delete-the-contents-of-a-folder) – SuperStormer Mar 25 '21 at 00:54
  • "it runs with no errors, but it doesn't actually do anything and I am failing to see why.." This is my question, why is it not deleting the folders in the test path? – Malenko Mar 25 '21 at 03:28
  • I will have a read through that link soon SuperStormer, I believe the answer may lie in there, thank you. – Malenko Mar 25 '21 at 03:30

1 Answers1

-1

Use shutil and recursively remove all old user directories and their contents.

import shutil
PATH = 'D:/user/bin/old_dir_to_remove'
shutil.rmtree(PATH)
  • I will do some testing with this and I will let you know how it goes. – Malenko Mar 25 '21 at 03:30
  • I apologize in advance for the formatting, I am still getting used to the stack overflow system. I have tried this with the following code. I created a folder called 'test' in my D drive and I encountered some traceback errors. import shutil path = 'D:/test' shutil.rmtree(path) – Malenko Mar 25 '21 at 04:11
  • Errors Traceback (most recent call last): File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Program Files\Python39\lib\py_compile.py", line 215, in sys.exit(main()) File "C:\Program Files\Python39\lib\py_compile.py", line 207, in main compile(filename, doraise=True) – Malenko Mar 25 '21 at 04:12
  • File "C:\Program Files\Python39\lib\py_compile.py", line 142, in compile source_bytes = loader.get_data(file) File "", line 979, in get_data FileNotFoundError: [Errno 2] No such file or directory: '' [Finished in 1.6s] – Malenko Mar 25 '21 at 04:12