思路就是先删除里面的文件,再删除文件夹,如果不直接删除文件夹会报错
def remove_dir(file):
for root, dirs, files in os.walk(file, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
if __name__ == '__main__':
file = 'C:\\xxxx\\xxxx\\xxx\\'
filepath, tempfilename = os.path.split(file)
if os.path.exists(filepath):
remove_dir(filepath)
shutil.rmtree(filepath)
本文介绍了一种使用Python批量删除指定路径下所有文件及其子文件夹的方法。通过递归遍历目标目录,先删除文件,再移除空文件夹,确保整个清理过程顺利进行。

被折叠的 条评论
为什么被折叠?



