zoukankan      html  css  js  c++  java
  • 遍历文件夹并删除特定格式文件的示例

     1 #!/usr/bin/python
     2 # -*- coding: utf-8 -*-
     3 import os
     4 def del_files(path):
     5     for root , dirs, files in os.walk(path):
     6         for name in files:
     7             if name.endswith(".tmp"):
     8                 os.remove(os.path.join(root, name))
     9   print ("Delete File: " + os.path.join(root, name))
    10 # test
    11 if __name__ == "__main__":
    12     path = '/tmp'
    13     del_files(path)

    if name.endswith(".tmp"):
       os.remove(os.path.join(root, name))

    补充:

    函数:endswith()

    作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型

    相关函数:判断字符串开头 startswith()

    一、函数说明 语法:string.endswith(str, beg=[0,end=len(string)])            string[beg:end].endswith(str)

    参数说明:

    string: 被检测的字符串

    str:      指定的字符或者子字符串(可以使用元组,会逐一匹配)

    beg:    设置字符串检测的起始位置(可选,从左数起)

    end:    设置字符串检测的结束位置(可选,从左数起) 如果存在参数 beg 和 end,则在指定范围内检查,否则在整个字符串中检查   

    返回值: 如果检测到字符串,则返回True,否则返回False。

    解析:如果字符串string是以str结束,则返回True,否则返回False

    注:会认为空字符为真

  • 相关阅读:
    JavaScript讲义(三)
    jQuery学习(五)
    jQuery学习(七)
    JavaScript讲义(一)
    jQuery学习(六)
    这些东西不宜空腹吃[转]
    pexpect实现的ssh连接(pexpect可从sourceforge下载)
    Using the commandline connection tool Plink
    【转】系统管理类DOS命令汇总
    挺有意思的QQ资料
  • 原文地址:https://www.cnblogs.com/gistwz/p/7928451.html
Copyright © 2011-2022 走看看