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 ...
随机推荐
- .Net转前端
坚持 OR 方向 转前端,去折腾CSS JS 各种神奇的移动端框架: Web App H5 前端 前端工程师=javascript+N种技能,即一专多长: JavaScript 世界,Node.js, ...
- android Handler错误,不同的包Handler
1. import java.util.logging.Handler;这个包了会自动生成如下方法.当时还觉得和以前的不一样了,本不在意. Handler handler1= new Handler( ...
- DataGridView单元格合并
本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...
- oracle连接由于防火墙设置导致超时的问题
当应用程序使用数据库连接池进行数据连接时,防火墙的设置有可能会导致连接出现超时或者被重置的问题.当从数据库读数据的时候 有可能会 Connection timed out, 这是由于应用会缓存 ...
- Codeforces Round #333 (Div. 2) A. Two Bases 水题
A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...
- 【Bootstrap3.0建站笔记三】AspNetPager分页,每一列都可排序
1.AspNetPager分页,实现每一列都可排序: (1).须要将默认排序字段放在HTML页面中. (2).排序字段放置为td节点的属性. 如图: 实现的效果 ...
- Ubuntu下多服务器 Rsync同步镜像服务配置
主服务器:192.168.5.13_ubuntu 从服务器:192.168.5.11_centos ================== 1> 在两台主机上分别安装rsync========== ...
- c语言海量数据处理
教你如何迅速秒杀掉:99%的海量数据处理面试题 http://wenku.baidu.com/view/4546d06ca45177232f60a276.html c语言如何对海量数据进行处理 PDF ...
- Skip list--reference wiki
In computer science, a skip list is a data structure that allows fast search within an ordered seque ...
- do {...} while (0) in macros
If you are a C programmer, you must be familiar with macros. They are powerful and can help you ease ...