python遍历文件夹并删除特定格式文件的示例

yipeiwu_com5年前Python基础

复制代码 代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os

def del_files(path):
    for root , dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".tmp"):
                os.remove(os.path.join(root, name))
  print ("Delete File: " + os.path.join(root, name))

# test
if __name__ == "__main__":
    path = '/tmp'
    del_files(path)

相关文章

python中的decorator的作用详解

1、概念 装饰器(decorator)就是:定义了一个函数,想在运行时动态增加功能,又不想改动函数本身的代码。可以起到复用代码的功能,避免每个函数重复性编写代码,简言之就是拓展原来函数功...

python getopt详解及简单实例

 python getopt详解 函数原型: getopt.getopt(args, shortopts, longopts=[]) 参数解释: args:args...

Python httplib模块使用实例

httplib模块是一个底层基础模块,实现的功能比较少,正常情况下比较少用到.推荐用urllib, urllib2, httplib2. HTTPConnection 对象 class...

Python 使用PIL numpy 实现拼接图片的示例

python纵向合并任意多个图片,files是要拼接的文件list # -*- coding:utf-8 -*- def mergeReport(files): from PIL...

Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解

本文实例讲述了Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法。分享给大家供大家参考,具体如下: 1. 定义类 python中定义一个类的格式如下: class...