python遍历
#coding=utf-8
#遍历的2种方式 import os #1.使用os.listdir(f) def traverse(f):
fs = os.listdir(f)
for f1 in fs:
tmp_path = os.path.join(f,f1)
if not os.path.isdir(tmp_path):
print('文件: %s'%tmp_path)
else:
print('文件夹:%s'%tmp_path)
traverse(tmp_path) path = 'F:/source_files/python/'
traverse(path) #2.使用os.walk
path = 'F:/source_files/python/' for fpathe,dirs,fs in os.walk(path):
for f in fs:
print(os.path.join(fpathe,f))
#coding=utf-8
'''
Created on 2018年8月28日 @author: yanerfree 获取某一目录下所有的sql文件
'''
import os
import shutil def traverse(file_path,save_fath):
list = os.listdir(file_path)
for i in range(0,len(list)):
#print list[i]
tmp_path = os.path.join(file_path,list[i])
#print tmp_path
if os.path.isfile(tmp_path):
if str(list[i])[-3:] == "sql":
#复制改文件到指定目录下
#print "sql文件",list[i]
save_as = os.path.join(save_fath,str(list[i]))
#print save_as
shutil.copyfile(tmp_path, save_as) else:
traverse(tmp_path,save_fath) if __name__ == '__main__':
starttime=datetime.datetime.now().microsecond file_path = r'./DCKeyMgrSystemExts'
save_fath = r'./sql'
traverse(file_path, save_fath) endtime=datetime.datetime.now().microsecond
costtime=endtime-starttime
print "totally cost %d ms"%costtime
案例2:
#coding=utf-8
'''
Created on 2018年8月28日 @author:yanerfree get the version number of the file (.dll) in bulk
批量获取dll文件的版本号 '''
import os
import sys
import win32api
import xlwt
from xlwt import * class GetFileVersionNo(): def __init__(self,file_path,save_path):
#初始化
self.file_path = file_path
self.save_path = save_path def traverse_dir(self,file_path):
#traverse the directory of file_path(the file are .dll)
myList=[]#save result
list = os.listdir(file_path)
for i in range(0,len(list)):
print list[i]
tmp_path = os.path.join(file_path,list[i])
print tmp_path
if os.path.isfile(tmp_path):
#if str(list[i]).split(".")[1] =="dll":
if str(list[i])[-3:] == "dll":
#judge if the filename ended with ".dll"
#print tmp_path,getFileVersion(tmp_path)
myList.append((list[i],self.getFileVersion(tmp_path))) return myList def getFileVersion(self,file_name):
#get the version of file
info = win32api.GetFileVersionInfo(file_name, os.sep)
ms = info['FileVersionMS']
ls = info['FileVersionLS']
version = '%d.%d.%d.%04d' % (win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls))
print version
return version def writeToExcel(self):
file_path = self.file_path
save_path = self.save_path
#write to excel
print "create a workbook"
book = Workbook(encoding='utf-8')#create a workbook
sheet = book.add_sheet('Sheet1')#create a sheet
#set style
font = xlwt.Font() # 字体
font.name = 'Times New Roman'
font.bold = True
font.underline = False
font.italic = False
style = xlwt.XFStyle() # 创建一个格式
style.font = font # 设置格式字体 #sheet.write(0, 0, label = 'Formatted value', style) # Apply the Style to the Cell
sheet.write(0, 0, "no",style)
sheet.write(0, 1, "file_name",style)
sheet.write(0, 2, "file_version",style) list = self.traverse_dir(file_path)
row=0
num=0
for item in list:
row += 1
num += 1
sheet.write(row, 0, num, style)
sheet.write(row, 1,item[0] , style)
sheet.write(row, 2,item[1] , style) book.save(save_path) if __name__ == '__main__':
#1)获取指定目录下dll文件的版本号
file_path1 = r'../01_dev/Exts'
save_path1 = r'../04_result/result_dev.xls'
getVersion1 = GetFileVersionNo(file_path1,save_path1)
getVersion1.writeToExcel()
python遍历的更多相关文章
- 用Python遍历目录
用Python遍历指定目录下的文件,一般有两种常用方法,但它们都是基于Python的os模块.下面两种方法基于Python2.7,主要用到的函数如下: 1.os.listdir(path):列出目录下 ...
- python 遍历文件夹 文件
python 遍历文件夹 文件 import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...
- python遍历目录文件脚本的示例
例子 自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理.没啥技术含量,但是也记录一下吧. 代码如下 复制代码 #!/usr/bin/python# -*- coding: utf-8 ...
- python遍历一个目录,输出所有文件名
python遍历一个目录,输出所有文件名 python os模块 os import os def GetFileList(dir, fileList): newDir = dir if os. ...
- Python遍历List集合四种方法
这篇文章主要介绍了Python 列表(List) 的四种遍历方法实例 详解的相关资料,需要的朋友可以参考下 分别是:直接遍历对象 通过索引遍历 通过enumerate方法 通过iter方法. 使用Py ...
- [python]python 遍历一个list 的小例子:
[python]python 遍历一个list 的小例子: mlist=["aaa","bbb","ccc"]for ss in enume ...
- python 遍历list并删除部分元素
python 遍历list并删除部分元素https://blog.csdn.net/afgasdg/article/details/82844403有两个list,list_1 为0-9,list_2 ...
- Python遍历文件夹
许多次需要用python来遍历目录下文件, 这一次就整理了记录在这里. 随实际工作,不定期更新. import os class FileTraversal: def __init__(self, r ...
- 【python】Python遍历dict的key最高效的方法是什么?
来源:https://segmentfault.com/q/1010000002581747 方法一:直接遍历 速度快 for key in _dict: pass 方法二:iterkeys() 速度 ...
- python遍历文件夹下的文件
在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...
随机推荐
- 2020年ubuntu1804安装nginx最新稳定版1.16详细教程笔记
第一次使用nginx是2007年,当时主流还是apache.nginx横空出世,在web2.0的推动下,迅速崛起.眼下已是绝对的主流了. 当时,还有一个轻量级的lighttpd,是德国人写,刚开始还并 ...
- dos命令下安装pip报错 不是内部命令
在dos命令下: pip install requests 遇到这种情况一般是Python的环境变量没有设置好 解决方案一:设置环境变量 C:\Python\scripts 如图 是否有pytho ...
- KNN (K近邻算法) - 识别手写数字
KNN项目实战——手写数字识别 1. 介绍 k近邻法(k-nearest neighbor, k-NN)是1967年由Cover T和Hart P提出的一种基本分类与回归方法.它的工作原理是:存在一个 ...
- 阿里云服务器连接AWS-S3
1.找到一个路径下载 aws-cli (使用离线包安装) wget -P /usr/local/software https://s3.amazonaws.com/aws-cli/awscli-bu ...
- ES[7.6.x]学习笔记(六)分析器
在前面的章节中,我们给大家介绍了索引中的映射类型,也就是每一个字段都有一个类型,比如:long,text,date等.这和我们的数据库非常的相似,那么它的不同之处是什么呢?对了,就是全文索引,在ES当 ...
- spring表达式语言
使用文本表达式 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http: ...
- 使用JDBC连接oracle数据库
需要jar包:ojdbc6.jar
- java基础篇 之 异常丢失
我们看如下代码: @Slf4j public class Test { public static void main(String[] args) { try { try { test(); } f ...
- maven项目变成web项目
具体步骤如图所示: 第一步:建议一个Maven Webapp项目 第二步:右击项目,选择属性,找到project facets,点击tuntimes标签选择apache tomcat v6.0选中P ...
- 【Spark】部署流程的深度了解
文章目录 Spark核心组件 Driver Executor Spark通用运行流程图 Standalone模式运行机制 Client模式流程图 Cluster模式流程图 On-Yarn模式运行机制 ...