folders not deeleted with errors [WinError 145] The directory is not empty

194 Views Asked by At

python scripts:

import os 
import datetime

# list of directories to monitor
directories = ['C:\_SETUPS\x1_Build', 'C:\_SETUPS\x2_Build']

# get today's date
today = datetime.datetime.now()

# calculate the date five days ago
five_days_ago = today - datetime.timedelta(days=5)

# loop through directories
for directory_path in directories:
    # loop through folders in the directory
    for foldername in os.listdir(directory_path):
        folderpath = os.path.join(directory_path, foldername)

        # check if the folder is older than three days
        folder_modified_time = datetime.datetime.fromtimestamp(os.path.getmtime(folderpath))
        if folder_modified_time < five_days_ago and os.path.isdir(folderpath):
            os.rmdir(folderpath)
       

error:

>python -m HouseKeeping.py
Traceback (most recent call last):
  File "C:\Programme\Python35\lib\runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "C:\Programme\Python35\lib\runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "C:\_SHARED\HouseKeeping.py", line 22, in <module>
    os.rmdir(folderpath)
OSError: [WinError 145] The directory is not empty: 'C:\\_SETUPS\\x1\\5.27.1.49671'

enter image description here

0

There are 0 best solutions below