zoukankan      html  css  js  c++  java
  • 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,如果是就删除。

  • 相关阅读:
    服务器切换
    闭包函数
    函数对象+嵌套
    lvs讲解
    了解python
    rang enumerate
    set-集合功能介绍
    元组-tuple功能介绍
    dict-字典功能介绍
    list-列表功能介绍
  • 原文地址:https://www.cnblogs.com/lgh344902118/p/6890321.html
Copyright © 2011-2022 走看看