python遍历当前目录并删除某文件


#coding: utf-8
"""
    this programe is to clear driverlog below this dir
    __author__:the_new_one
"""
import os, traceback

#查找文件名中包含关键词的文件
def search_dir(s, path=os.path.abspath('.'),files = []):
    try:
        for x in os.listdir(path):
            path_now = os.path.join(path, x)
            if os.path.isfile(path_now) and s in os.path.splitext(x)[0]:
                print path_now
                #删除查找到的文件
                os.remove(path_now)
                if x not in files:
                    files.append(x)
            elif os.path.isdir(x):
                search_dir(s=s, path=os.path.join(path_now), files=files)
        return files
    except Exception, e:
        print traceback.format_exc()
        print e

if __name__ == "__main__":
    result = search_dir(s='xxx')

通过遍历当前路径下的文件,判断文件名是否包含s,如果是就删除。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM