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 ...
随机推荐
- oracle查询当前系统时间前10天的数据
select * from eo_c_order t where t.create_time>systimestamp-interval'1'day; 转载于:https://www.cnblo ...
- log4net进阶手札(二):基本用法
本节将主要在WebSite中,对保存日志在文本文件的基本用法来进行介绍,并结合WebForm的初始化方式区别进行说明,解决方案如下图所示: 一.WebSite应用第1步:配置Web.Config文件, ...
- 导入sql错误
2019独角兽企业重金招聘Python工程师标准>>> 导入sql错误: This function has none of DETERMINISTIC, NO SQL, or RE ...
- 04 全局局部配置 wxml数据绑定 事件 冒泡
一. 配置介绍 一个小程序应用程序会包括最基本的两种配置文件.一种是全局的 app.json 和 页面自己的 page.json(index.json /test.json等) 注意:配置文件中不能出 ...
- 06 ORM常用字段 关系字段 数据库优化查询
一.Django ORM 常用字段和参数 1.常用字段 models中所有的字段类型其实本质就那几种,整形varchar什么的,都没有实际的约束作用,虽然在models中没有任何限制作用,但是还是要分 ...
- Hello World的五十种不同实现方法!!!!!
我们作为一名程序员,职业生涯中至少完成了一个“Hello, World!“程序.当我们学习一门新的语言时,“Hello, World!“通常是我们所写的第一个程序.程序员一般也都会使用多门语言,甚至有 ...
- 僵尸进程(zombie process)
首先了解一下linux中进程的5大状态: R Running or runnable (on run queue)S Interruptible sleep (waiting for an event ...
- 麦基数(p1045)
描述: \(计算2^{P}−1的位数和最后500位数字(用十进制高精度数表示)\) Ⅰ.求位数 \(因为2^p最后一位必定不为0,求2^p-1的位数也就是求2^p位数\) \(2^p的位数确实很难求, ...
- Oracle触发器之替代触发器
替代触发器 替代视图增删改操作.视图可以认为成逻辑上的一张表,类似于把一个sql语句的执行结果永久的像表存储到数据 库中,视图一般用来做查询. 创建视图的语法: create view 视图名称 as ...
- Oracle的pl/sql变量类型
pl/sql定义 sql是结构化查询语言.sql是不是一个编程语言?编程语言一般都能够声明变量,写条件判断,循环.sql不具备这些特征,所有sql不是一门编程语言.我们在实际的开发中,有这种需要,把s ...