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 ...
随机推荐
- mac OS 查看开机/关机/重启记录
last 查看最近的开关机.登录用户等记录 以及操作时间节点. last | grep reboot 查看重启记录 last | grep shutdown 查看关机记录
- angular2相关
脚手架安装一个项目 1.全局安装angular脚手架 npm install -g @angular/cli 2.初始化一个文件夹 ng new my-angular-demo 3.进入文件夹 cd ...
- inotifywait实现文件监控
应用场景文件监控可以配合rsync实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步.(可用于代码自动发布) 安装noitify下载地址:http://github. ...
- undef用法
#undef的语法 定义:#undef 标识符,用来将前面定义的宏标识符取消定义. 整理了如下几种#undef的常见用法. 1. 防止宏定义冲突在一个程序块中用完宏定义后,为防止后面标识符冲突需要取消 ...
- ModuleNotFoundError: No module named 'pip'的解决方案
python在通过cmd安装pandas时遇到ModuleNotFoundError: No module named 'pip'的报错. 网上查询后通过如下两行cmd命令解决了 python -m ...
- D. Equalize the Remainders set的使用+思维
D. Equalize the Remainders set的学习::https://blog.csdn.net/byn12345/article/details/79523516 注意set的end ...
- NetCore项目实战篇05---添加Ocelot网关并集成identity server4认证
今天来给我们的项目增加API网关,使用Ocelot. 它是系统暴露在外部的一个访问入口,这个有点像代理访问的家伙,就像一个公司的门卫承担着寻址.限制进入.安全检查.位置引导.等等功能.同时我们还要在网 ...
- Python 文件的读取与写入
1. 读取文件,文件中没有中文 备注 : 文件名 : EnglishFile.txt 文件位置 : 保存在所写的.py文件的同级目录,附上截图,便于参考 备注 : 文件位置可以改变,只需要把文件路径传 ...
- 【Elasticsearch学习】文档搜索全过程
在ES执行分布式搜索时,分布式搜索操作需要分散到所有相关分片,若一个索引有3个主分片,每个主分片有一个副本分片,那么搜索请求会在这6个分片中随机选择3个分片,这3个分片有可能是主分片也可能是副本分片, ...
- Ubuntu 拦截并监听 power button 的关机消息
system:ubuntu 18.04 platform:rockchip 3399 board:NanoPi M4 前言 物理上的电源按键短按之后,系统直接硬关机了,导致应用程序无法保护现场,就直接 ...