python遍历文件夹,作用很大

2019年3月29日00:37:37 发表评论 1,186 views

之前就想写一个遍历文件夹的代码,这样可以删除垃圾文件,文件夹同步,文件筛选,文件过滤,文件分类,文件搜索整理等功能,奈何无法遍历子文件夹,前几天折腾出来了,今天刚好要交作业,可以试试,可能有点bug,以后再调试,代码如下:

import os
paths = 'D:/'
# dirss = []
def second_dirs(path,dirs = []):
    for i in os.listdir(path):
        path = path+'\\'+i
        dirs.append(path)
        if os.path.isdir(path):
            second_dirs(path, dirs)
    return dirs


#调用函数,搞点成就感
for i in second_dirs(paths):
    print(i)

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: