zoukankan      html  css  js  c++  java
  • 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.111cn.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)
     

    更多详细内容请查看:http://www.111cn.net/phper/python/58530.htm

  • 相关阅读:
    1.3 Starting a New Job 1.3.3 Background Reading(I)
    1.3 Starting a New Job 1.3.1 Preparation
    1.2 Interview 1.2.4 Sample Test(IV)
    1.2 Interview 1.2.4 Sample Test(III)
    Jquery获取元素集合并遍历
    Oracle21c RAC+DG生产项目实战(RHEL8+Oracle集群+容灾+CDB/PDB)
    Django
    Linux 实用命令
    Python人工智能:原理、实践及应用-资料分享
    关于HDFS的NameNode和SecondaryNameNode的一些疑问解答
  • 原文地址:https://www.cnblogs.com/alibai/p/3585737.html
Copyright © 2011-2022 走看看