Search code examples
pythonfile-handlingshutilpython-os

why python is dropping errors after execution is being completed


I write the program that penetrate inside the folder and check if folder have sub folders that either contained files or not if there is folders which contained files inside program penetrate into it again and delete all founded files in order to make folders empty and get ready to remove it but after the program being executed i get the following error when there is many folders and files inside the directory

PermissionError: [WinError 5] Access is denied: 'FileLocation\\Folder'

and if the directory name inside that directory has two or more words with space separator between them the program runs well but drop the following error

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'FileLocation\\firstName secondName'

and the codes which I wrote with the only os module are the one that follows

import os

# parent folder
New = "C:\\Users\\HP\\Desktop\\New"

# check if the path exists
if os.path.exists(New):
# looping over all files and directories inside the parent folder
for files in os.listdir(New):
    # check if there is directories
    if os.path.isdir(New + '\\' + files):
        # check if the directories are empty and get ready to remove it
        if len(os.listdir(New + '\\' + files)) <= 0:
            os.rmdir(New + '\\' + files)
        # when directories are not empty
        else:
            # search for file inside the nested directories
            for sub_files in os.listdir(New + '\\' + files):
                # remove all the files inside the nested directories
                os.remove(New + '\\' + files + "\\" + sub_files)
        # remove directories after removing files inside it
        os.removedirs(New + '\\' + files)
    # check if there is files
    if os.path.isfile(New + '\\' + files):
        # removing the files inside the parent folder
        os.remove(New + '\\' + files)
# removing the entire folder after deleting all files and folders inside it
os.rmdir(New)
else:
    print('Folder doesn\'t exist')

while when i wrote the program like the second following codes work well without any logic or runtime error and its codes are the following with the shutil and os modules

import shutil
import os

# parent folder
New = "C:\\Users\\HP\\Desktop\\New"
if os.path.exists(New):
     shutil.rmtree(New)
else:
    print('Folder doesn\'t exist')

so i would like to know if there is any further configuration about the python errors or any bugs in my codes to fix or any way drops no error at runtime which will be better for this(removing directories which is not empty) thanks


Solution

  • I believe it's this part:

        if len(os.listdir(New + '\\' + files)) <= 0:
            os.rmdir(New + '\\' + files)
        # when directories are not empty
        else:
            # search for file inside the nested directories
            for sub_files in os.listdir(New + '\\' + files):
                # remove all the files inside the nested directories
                os.remove(New + '\\' + files + "\\" + sub_files)
    

    You have if len(os.listdir(New + '\\' + files)) <= 0: which means if the folder is empty.
    You also have else:, which is for non-empty folders.
    But, if in that folder, there is another folder that is also not empty,
    you cannot call os.remove(New + '\\' + files + "\\" + sub_files) on that,
    so it throws a PermissionError: [WinError 5] Access is denied error.