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 ...
随机推荐
- Leetcode2 两数相加 Python
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- CF1335F Robots on a Grid
比较简单的倍增 但还是看了题解才会 题意 给出一个 \(n\times m\) 的网格,每个格子有颜色,\(0\) 黑 \(1\) 白,每个格子还有一个方向,表示这个格子上的机器人会向那个方向走,并保 ...
- Python+wxpy 实现微信消息轰炸
需要导入wxpy,在终端中输入以下命令即可 pip install wxpy 如果没有pip先安装pip,安装好了的直接输入命令即可,安装好了但是显示没有安装的可能是没有将pip添加到PATH中,需要 ...
- Flutter 系统是如何实现ExpansionPanelList的
老孟导读:Flutter组件有一个很大的特色,那就是很多复杂的组件都是通过一个一个小组件拼装而成的,今天就来说说系统的ExpansionPanelList是如何实现的. 在了解ExpansionPan ...
- 【Java新特性】Lambda表达式典型案例,你想要的的都在这儿了!!
写在前面 不得不说,有些小伙伴的学习热情真高,学完了Lambda表达式的语法,想来几个典型案例再强化下.于是问冰河能否给几个Lambda表达式的典型使用示例.于是乎,便有了这篇文章. 案例一 需求 调 ...
- Spring中资源的加载原来是这么一回事啊!
1. 简介 在JDK中 java.net.URL 适用于加载资源的类,但是 URL 的实现类都是访问网络资源的,并没有可以从类路径或者相对路径获取文件及 ServletContext , 虽然可以通过 ...
- A - Aragorn's Story HDU - 3966 树剖裸题
这个题目是一个比较裸的树剖题,很好写. http://acm.hdu.edu.cn/showproblem.php?pid=3966 #include <cstdio> #include ...
- Vue列表实现滚动到指定位置样式改变
这个需求大概是这样子: 我做的一个聊天Demo,在搜索框搜索用户,可以滚动到指定的用户.然后成选中状态. 这是目前状态,我搜索 南宫仆射 ,想要下面的用户列表直接滚动到 南宫仆射 并改变CSS样式. ...
- Zuul源码分析
先上一张流程图: 我们Zuul的使用如下: @SpringBootApplication @EnableZuulProxy public class ZuulApplication { public ...
- zigbee通用IO口小作业
独立新建工程并编写.编译代码,实现按键控制流水灯运行,完成以下任务要求: [1]程序开始运行时,D4.D3.D6.D5灯全亮一会,然后全灭一会,开始进入流水灯. [2]流水灯的运行过程为:D4灯亮,其 ...