Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python遍历目录文件脚本的示例

例子自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理。没啥技术含量,但是也记录一下吧。代码如下 复制代码#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import shutil
dir = "/mnt/Packages"
class Packages:
    def __init__(self,srcdir,desdir):
        self.sdir=srcdir
        self.ddir=desdir
    def check(self):
        print("program start...")
        for dirpath
 
, dirnames, filenames in os.walk(self.sdir):  www.1linuxidc.Net  #遍历文件
            for filename in filenames:
                thefile=os.path.join(dirpath,filename)            #文件的绝对地址
                try:
                    if os.path.splitext(thefile)[1]==".rpm":      #筛选.rpm格式的文件
                        #print("Fount rpm package: " + thefile)
                        if "inspuer" in os.popen("rpm -qpi " + thefile).read().rstrip():
                            print("Found error package: " + thefile)
                            shutil.copy(thefile, self.ddir)  #将错误文件复制到desdir目录
                            f = open("list.txt", "a")    #将错误文件列表写入到list.txt
                            f.write(filename + " ")
                            f.close()
                except IOError, err:
                    print err
                    sys.exit()
 
if __name__ == "__main__":
    dir=Packages("/mnt/cdrom","/mnt/erpm") #源目录为/mnt/cdrom,目标目录为/mnt/erpm
    dir.check()例子,遍历目录下文件代码如下 复制代码def search(folder, filter, allfile):
    folders = os.listdir(folder)
    for name in folders:
        curname = os.path.join(folder, name)
        isfile = os.path.isfile(curname)
        if isfile:
            ext = os.path.splitext(curname)[1]
            count = filter.count(ext)
            if count>0:
                cur = myfile()
                cur.name = curname
                allfile.append(cur)
        else:
            search(curname, filter, allfile)
    return allfile例子遍历文件夹并删除特定格式文件代码如下 复制代码#!/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的文章您也可能喜欢,不妨看看:Linux下Python的安装以及注意事项  http://www.linuxidc.com/Linux/2015-11/124861.htmUbuntu 14.04 下安装使用Python rq模块  http://www.linuxidc.com/Linux/2015-08/122441.htm无需操作系统直接运行 Python 代码  http://www.linuxidc.com/Linux/2015-05/117357.htmCentOS上源码安装Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里 本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/126838.htm