简体   繁体   English

为什么 python 在执行完成后会丢弃错误

[英]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我用唯一的os 模块编写的代码是下面的代码

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而当我像下面的第二个代码编写程序时,它运行良好,没有任何逻辑或运行时错误,其代码与shutil 和 os 模块一起使用

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所以我想知道是否有任何关于 python 错误的进一步配置或我的代码中的任何错误要修复或任何方式在运行时不会出现错误,这将更好(删除不为空的目录)谢谢

Click right button on Pychrarm or CMD whatever you use to run the script and select Run as a System administrator在 Pychrarm 或 CMD 上单击右键以运行脚本和 select 以系统管理员身份运行

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.你有if len(os.listdir(New + '\\' + files)) <= 0:这意味着如果文件夹是空的。
You also have else: , which is for non-empty folders.您还有else: ,用于非空文件夹。
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,你不能调用os.remove(New + '\\' + files + "\\" + sub_files)
so it throws a PermissionError: [WinError 5] Access is denied error.所以它抛出一个PermissionError: [WinError 5] Access is denied错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM