Python_练习_VS清理器

#导入os
import os
#创建列表放入后缀
d=[ '.txt','obj','tlog','lastbuildstate','idb','pdb','pch','res','ilk','sdf','ipch']
#定义函数,参数为路径
def fun(path):
    #创建列表放要删除的文件
    l_del=[]

    try:
        #获取path路径里的文件跟文件夹,file好像是一个列表
        file = os.listdir(path)
        for f in file:
            #   f好像是一个str或者bytes
            #join函数参数(前一个路径、逗号、要添加的文件名称)
            if os.path.isdir(os.path.join(path,f)):
                #拼接路径
                p=os.path.join(path,f)
                #测试路径是否正确
                print(p)
                #递归,如果还是目录就继续打开
                fun(p)
            else:
                #获取后缀名,但是获取到的后缀名有个点,即“.txt”
                name = os.path.splitext(f)
                #测试
                print(name)
                #循环遍历,如果有相同就添加到列表
                for i in d:
                    if name[1] == i:
                        l_del.append(f)

                        break
    #异常分析,e可以用来保存错误信息帮助分析
    except Exception as e:
        print('-----------')
        print(path)
        print(e)
        print('-----------')
    print('-----------')
    print(path)
    print(l_del)
    print('-----------')
#开头的r是表示自然串,可以让反斜杠不是转义符号。
fun(r'e:\abc')

猜你喜欢

转载自www.cnblogs.com/ltyandy/p/10981753.html