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
python遍历目录文件脚本的示例的更多相关文章
- Python遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例
遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例: #-*- encoding: utf-8 -*- __author__ = 'liudong' import linecache,sys ...
- Python 遍历目录下的子目录和文件
import os A: 遍历目录下的子目录和文件 for root,dirs ,files in os.walk(path) root:要访问的路径名 dirs:遍历目录下的子目录 files:遍历 ...
- 用Python遍历目录
用Python遍历指定目录下的文件,一般有两种常用方法,但它们都是基于Python的os模块.下面两种方法基于Python2.7,主要用到的函数如下: 1.os.listdir(path):列出目录下 ...
- python遍历目录os.walk(''d:\\test2",topdown=False)
os.walk(top, topdown=True, onerror=None, followlinks=False)遍历目录,topdown=false表示先返回目录,后返回文件 参数说明: top ...
- c#调用api(FindFirstFile,FindNextFile)高效遍历目录文件【转载】
在c#下遍历目录,应用最多的应该就是 System.IO.DirectoryInfo.GetDirectories或GetFiles了,但是当目录特别大,文件特别多时,效率不尽人意,此时我们很容易想到 ...
- python模块目录文件后续
1,新增PythonModule加载path Ruiy tip(关于python list[]数据库类型特殊你懂的!append(""),extend([""] ...
- ZH奶酪:PHP遍历目录/文件的3种方法
其实PHP中内建函数scandir()就可以返回目录下全部文件和目录了... ========================== 1.使用$obj = dir($dir)返回目录对象$obj,然后使 ...
- Python遍历一个文件夹下有几个Excel文件及每个Excel文件有几个Sheet
一. 解决问题: 工作中常会遇到合并Excel文件的需求,Excel文件数量不确定,里面的Sheet 数量是可变的,Sheet Name是可变的,所以,需要用到遍历一个文件夹下有几个Excel文件,判 ...
- linux 遍历目录+文件(优化版本)
c++17 filesystem, regex 遍历目录 #include <stdio.h> #include <sys/types.h> #include <dire ...
随机推荐
- Javascript可变长度参数列表 - Arguments对象
在一个函数体内,标识符arguments具有特殊含义. Arguments对象是一个类似数组的对象 eg: 验证函数参数的正确数目 function f(x, y, z) { if (argument ...
- 跨平台实现wchar_t转成char
位宽.其实知道了这个以后,要在wchar_t 和 char两种类型之间转换就不难实现了. wchar_t 转换为char 的代码如下: 有如下的wchar_t和char变量 wchar_t w_cn ...
- delphi 文件或目录转换成 TreeView
//文件或目录转换成 TreeViewprocedure DirToTreeView(Tree: TTreeView; Directory: string; Root: TTreeNode; Incl ...
- key 限制字符的输入
//限制字符的输入 { 只能输入以下字符 } procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);begin If (Key ...
- ZJU-PAT 1065. A+B and C (64bit) (20)
Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Input S ...
- [Jest] Test JavaScript with Jest
Let's learn how to unit test your JavaScript with Jest, a JavaScript unit testing framework from Fac ...
- [AngularJS] Html ngSanitize, $sce
Safely render arbitrary HTML snippets by using ngSanitize and $sce. By default angularJS consider us ...
- VC++制作DLL具体解释
1. DLL的基本概念 应用程序(exe)要引用目标代码(.obj)外部的函数时,有两种实现途径--静态链接和动态链接. 1. 静态链接 链接程序搜索相应的库文件(.lib),然后将这个对 ...
- Ruby on Rails Tutorial 第二章 之 用户资源&MVC&REST
说明:用户资源包括用户数据模型和这个模型相关的Web页面. 1.用户数据模型如下: 2.使用Rails内置的脚手架生成用户资源中,执行如下所示命令: $ rails generate scaffold ...
- strassen algorithm
the explaination that is clear in my view is from wiki.