Python技巧:删除文件夹(包括文件夹)

https://www.cnblogs.com/dwtt/p/7772639.html

使用了递归

import os
def del_file(path):
    for i in os.listdir(path):
        path_file = os.path.join(path,i)  // 取文件绝对路径
        if os.path.isfile(path_file):
            os.remove(path_file)
        else:
            del_file(path_file)
    os.removedirs(path)
       

亲测如果不增加os.removedirs(path)的话不能删除最后空的文件夹

猜你喜欢

转载自blog.csdn.net/weixin_43826242/article/details/87101436