需要检查是否存在多个目录

时间:2018-12-16 15:08:59

标签: python python-3.x

def check_and_remove(pathslist):
    for path in pathslist:
        if os.path.exists(path) and os.path.isdir(path):
        shutil.rmtree(path, ignore_errors=true)
        print("Deleted")
    else:
        print(path, " directory not found")

dirs_to_delete = [
'C:\Directory1',
'C:\Directory2',
'C:\Directory3'
]

check_and_remove()

将建议的shutil.rmtree(dir,ignore_errors = true)更改为shutil.rmtree(path,ignore_errors = true)

现在出现此错误-

回溯(最近通话最近):   在第53行的“ C:\ Users \ Temp \ PycharmProjects \ crm \ CRMReinstall.py”文件中     check_and_remove() TypeError:check_and_remove()缺少1个必需的位置参数:“ pathslist”

2 个答案:

答案 0 :(得分:0)

shutil.rmtree(dir, ignore_errors=true)中,您正在使用dir,这是python内置函数,也许您想执行shutil.rmtree(path, ignore_errors=true)

答案 1 :(得分:0)

在该行中用dir替换path

shutil.rmtree(dir, ignore_errors=true)

dir是该语言的内置功能。<​​/ p>