首页 > 编程语言 > 详细

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

时间:2017-05-22 17:34:00      阅读:399      评论:0      收藏:0      [点我收藏+]
#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,如果是就删除。

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

原文:http://www.cnblogs.com/lgh344902118/p/6890321.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!