python遍历文件|Python中如何遍历指定目录下的所有文件

|

A. 如何利用Python遍历文件夹

import osimport os.pathrootdir = “d:\data” # 指明被遍历的文件夹for parent,dirnames,filenames in os.walk(rootdir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字for dirname in dirnames: #输出文件夹信息print "parent is:" + parentprint "dirname is" + dirnamefor filename in filenames: #输出文件信息print "parent is": + parentprint "filename is:" + filenameprint "the full name of the file is:" + os.path.join(parent,filename) #输出文件路径信息#windows下为:d:\data\query_text\EL_00154

B. python如何遍历当前目录下所有文件,并输出后缀不为 py的文件名称

#coding=utf-8'''Created on 2014-11-14 @author: Neo'''import os def GetFileList(dir, fileList): newDir = dir if os.path.isfile(dir): fileList.append(dir.decode('gbk')) elif os.path.isdir(dir): for s in os.listdir(dir): #如果需要忽略某些文件夹,使用以版下权代码 #if s == "xxx": #continue newDir=os.path.join(dir,s) GetFileList(newDir, fileList) return fileList list = GetFileList('D:\\workspace\\PyDemo\\fas', [])for e in list: print e

C. python如何实现定时遍历目录,每次把目录下的最新的文件放到列表里

defgetFileListType(path,deal_type_dict):'定时每分钟调用此函数'new_file_list=os.listdir(path)fornew_fileinnew_file_list:#判断是否是新进来的文件ifnew_fileindeal_type_dict:contiunedeal_type_dict[new_file]=0#0表示文件未下一步操作1表示已处理if__name__=='__main__':deal_type_dict={}while1:time.sleep(60)getFileListType("A目录")fordeal_fileindeal_type_dict:ifdeal_type_dict[deal_file]==0:deal_next_function(deal_file)#新文件处理操作deal_type_dict[deal_file]=1#并标识处理过的文件#如果要定时清理,自己实现一个计时函数清理字典中处理过的文件if满足条件==1天:清除字典中=1的记录

D. 如何用python遍历文件夹下的所有excel文件

大数据处理经常要用到一堆表格,然后需要把数据导入一个list中进行各种算法分析,简单讲一下自己的做法:

1.如何读取excel文件

网上的版本很多,在xlrd模块基础上,找到一些源码:

[python]view plain

importxdrlib,sys

importxlrd

defopen_excel(file="C:/Users/flyminer/Desktop/新建MicrosoftExcel工作表.xlsx"):

data=xlrd.open_workbook(file)

returndata

#根据索引获取Excel表格中的数据参数:file:Excel文件路径colnameindex:表头列名所在行的所以,by_index:表的索引

defexcel_table_byindex(file="C:/Users/flyminer/Desktop/新建MicrosoftExcel工作表.xlsx",colnameindex=0,by_index=0):

data=open_excel(file)

table=data.sheets()[by_index]

nrows=table.nrows#行数

ncols=table.ncols#列数

colnames=table.row_values(colnameindex)#某一行数据

list=[]

forrownuminrange(1,nrows):

row=table.row_values(rownum)

ifrow:

app={}

foriinrange(len(colnames)):

app[colnames[i]]=row[i]

list.append(app)

returnlist

#根据名称获取Excel表格中的数据参数:file:Excel文件路径colnameindex:表头列名所在行的所以,by_name:Sheet1名称

defexcel_table_byname(file="C:/Users/flyminer/Desktop/新建MicrosoftExcel工作表.xlsx",colnameindex=0,by_name=u'Sheet1'):

data=open_excel(file)

table=data.sheet_by_name(by_name)

nrows=table.nrows#行数

colnames=table.row_values(colnameindex)#某一行数据

list=[]

forrownuminrange(1,nrows):

row=table.row_values(rownum)

ifrow:

app={}

foriinrange(len(colnames)):

app[colnames[i]]=row[i]

list.append(app)

returnlist

defmain():

tables=excel_table_byindex()

forrowintables:

print(row)

tables=excel_table_byname()

forrowintables:

print(row)

if__name__=="__main__":

main()

最后一句是重点,所以这里也给代码人点个赞!

最后一句让代码里的函数都可以被复用,简单地说:假设文件名是a,在程序中import a以后,就可以用a.excel_table_byname()和a.excel_table_byindex()这两个超级好用的函数了。

2.然后是遍历文件夹取得excel文件以及路径:,原创代码如下:

[python]view plain

importos

importxlrd

importtest_wy

xpath="E:/唐伟捷/电力/电力系统总文件夹/舟山电力"

xtype="xlsx"

typedata=[]

name=[]

raw_data=[]

file_path=[]

defcollect_xls(list_collect,type1):

#取得列表中所有的type文件

foreach_elementinlist_collect:

ifisinstance(each_element,list):

collect_xls(each_element,type1)

elifeach_element.endswith(type1):

typedata.insert(0,each_element)

returntypedata

#读取所有文件夹中的xls文件

defread_xls(path,type2):

#遍历路径文件夹

forfileinos.walk(path):

foreach_listinfile[2]:

file_path=file[0]+"/"+each_list

#os.walk()函数返回三个参数:路径,子文件夹,路径下的文件,利用字符串拼接file[0]和file[2]得到文件的路径

name.insert(0,file_path)

all_xls=collect_xls(name,type2)

#遍历所有type文件路径并读取数据

forevey_nameinall_xls:

xls_data=xlrd.open_workbook(evey_name)

foreach_sheetinxls_data.sheets():

sheet_data=test_wy.excel_table_byname(evey_name,0,each_sheet.name)

#请参考读取excel文件的代码

raw_data.insert(0,sheet_data)

print(each_sheet.name,":Datahasbeendone.")

returnraw_data

a=read_xls(xpath,xtype)

print("Victory")

欢迎各种不一样的想法~~

E. 如何用Python os.path.walk方法遍历搜索文件内容的操作详解

本文是关于如何用Python os.path.walk方法遍历搜索文件目录内容的操作详解的文章,python 代码中用os.path.walk函数这个python模块的方法来遍历文件,python列出文件夹下的所有文件并找到自己想要的内容。文中使用到了Python os模块和Python sys模块,这两个模块具体的使用方法请参考玩蛇网相关文章阅读。Python os.path.walk方法遍历文件搜索内容方法代码如下:?041import os, sys#代码中需要用到的方法模块导入 listonly = False skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary files def visitfile(fname, searchKey): global fcount, vcount try: if not listonly: if os.path.splitext(fname)[1] in skipexts: pass elif open(fname).read().find(searchKey) != -1: print'%s has %s' % (fname, searchKey) fcount += 1 except: pass vcount += 1 #www.iplaypy.com def visitor(args, directoryName,filesInDirectory): for fname in filesInDirectory: fpath = os.path.join(directoryName, fname) if not os.path.isdir(fpath): visitfile(fpath,args) def searcher(startdir, searchkey): global fcount, vcount fcount = vcount = 0 os.path.walk(startdir, visitor, searchkey) if __name__ == '__main__': root=raw_input("type root directory:") key=raw_input("type key:") searcher(root,key) print 'Found in %d files, visited %d' % (fcount, vcount)

F. Python中如何遍历指定目录下的所有文件

例如:在C:\TDDOWNLOAD目录下有a.txt、b.txt两个文件,另有\sub1子文件夹,C:\TDDOWNLOAD\sub1下又有c.txt、d.txt两个文件。 1. os.walkos.walk()返回一个三元素的tuple:当前路径、子文件夹名称、文件列表。>>> import os>>> def fun( path ):… for root, dirs, files in os.walk( path ):… for fn in files:… print root, fn… >>> fun( r'C:\TDDOWNLOAD' )C:\TDDOWNLOAD a.txtC:\TDDOWNLOAD b.txtC:\TDDOWNLOAD\sub1 c.txtC:\TDDOWNLOAD\sub1 d.txt>>> 2. glob.globglob.glob()只接受一个参数,这个参数既代有路径,又代有匹配模式,返回值为一个列表。注意,glob.glob()无法直接穿透子文件夹,需要自己处理:>>> def fun( path ):… for fn in glob.glob( path + os.sep + '*' ): # '*'代表匹配所有文件… if os.path.isdir( fn ): # 如果结果为文件夹… fun( fn ) # 递归… else:… print fn… >>> fun( r'C:\TDDOWNLOAD' )C:\TDDOWNLOAD\a.txtC:\TDDOWNLOAD\b.txtC:\TDDOWNLOAD\sub1\c.txtC:\TDDOWNLOAD\sub1\d.txt>>> '*'为匹配模式,代表匹配所有文件,只有这样才能将子文件夹查出来,以便递归深入,探查下一层的文件。

G. python或者bat怎么遍历文件夹下所有文件和文件夹然后修改后缀

先遍历所有文件:

fromosimportwalkf=[]for(dirpath,dirnames,filenames)inwalk(mypath):f.extend(filenames)break

H. python:遍历文件并搜索/读取

os.walk()

I. Python 如何遍历服务器上所有同一类型的文件

例如:在C:\TDDOWNLOAD目录下有a.txt、b.txt两个文件,另有\sub1子文件夹,C:\TDDOWNLOAD\sub1下又有c.txt、d.txt两个文件。1. os.walkos.walk()返回一个三元素的tuple:当前路径、子文件夹名称、文件列表。>>> import os>>> def fun( path ):… for root, dirs, files in os.walk( path ):… for fn in files:… print root, fn… >>> fun( r'C:\TDDOWNLOAD' )C:\TDDOWNLOAD a.txtC:\TDDOWNLOAD b.txtC:\TDDOWNLOAD\sub1 c.txtC:\TDDOWNLOAD\sub1 d.txt>>>2. glob.globglob.glob()只接受一个参数,这个参数既代有路径,又代有匹配模式,返回值为一个列表。注意,glob.glob()无法直接穿透子文件夹,需要自己处理:>>> def fun( path ):… for fn in glob.glob( path + os.sep + '*' ): # '*'代表匹配所有文件… if os.path.isdir( fn ): # 如果结果为文件夹… fun( fn ) # 递归… else:… print fn… >>> fun( r'C:\TDDOWNLOAD' )C:\TDDOWNLOAD\a.txtC:\TDDOWNLOAD\b.txtC:\TDDOWNLOAD\sub1\c.txtC:\TDDOWNLOAD\sub1\d.txt>>>'*'为匹配模式,代表匹配所有文件,只有这样才能将子文件夹查出来,以便递归深入,探查下一层的文件。

J. 求教python怎么遍历指定文件

监控目录 — 分两个部分: 1. 扫描目录文件, 保持当前状态数据; 2. 状态数据的比较

importosimportfnmatchdefgetfileinfo(filename):(mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)=os.stat(filename)returndict(modifytime=mtime,createtime=ctime,size=size,)classDirectoryMonitor(object):def__init__(self,path,fnexp="*.*"):self.path=pathself.fnexp=fnexpself.files={}self.scan()defscan(self):currentfiles={}forpath,dirs,filesinos.walk(self.path):forfinfnmatch.filter(files,self.fnexp):fullname=os.path.join(path,f)currentfiles[fullname]=getfileinfo(fullname)lastfiles=self.filesself.files=currentfilesreturnself.check(lastfiles,currentfiles)@staticmethoddefcheck(lastfiles,currfiles):monitor={}newer={}forfinset(currfiles)-set(lastfiles):newer[f]=currfiles[f]ifnewer:monitor["newer"]=newerdeleted={}forfinset(lastfiles)-set(currfiles):deleted[f]=lastfiles[f]ifdeleted:monitor["deleted"]=deletedchanged={}forfinset(lastfiles)&set(currfiles):iflastfiles[f]!=currfiles[f]:changed[f]=currfiles[f]ifchanged:monitor["changed"]=changedreturnmonitordeftester():importtimedm=DirectoryMonitor(r"/home/tim/data","*.txt")time.sleep(20)m=dm.scan()ifm:printmif__name__=="__main__":tester()


赞 (0)