python中的os.walk 遍历文件函数为什么遍历带有中文的路径时就失败

编程语言 python(25)
python库函数 os.walk 遍历目录下的所有的文件。
#-*- encoding:UTF-8 -*-
#r=input(&type a directory name:&)
for root,dirs,files in os.walk('.'):
print 'haha', (root,dirs,files)
for f in files:
print 'file' , os.path.join(root,f), (root,f)
# 这个方法返回的是一个三元tupple(dirpath, dirnames, filenames),
#其中第一个为起始路径,
#第二个为起始路径下的文件夹,
#第三个是起始路径下的文件.
#dirpath是一个string,代表目录的路径,
#dirnames是一个list,包含了dirpath下所有子目录的名字,
#filenames是一个list,包含了非目录文件的名字.这些名字不包含路径信息,如果需要得到全路径,需要使用 os.path.join(dirpath, name).
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:49074次
排名:千里之外
原创:43篇
(3)(3)(3)(8)(7)(17)(4)Python读取文件目录树——os.walk
os.walk是的内置函数用来遍历文件目录树。
import os&
rootDir = 'd:\\assa'&
for dirName, subdirList, fileList in os.walk(rootDir):&
&&& print('Folder: %s' % dirName)&
&&& for fname in fileList:&
&&&&&&& print('\t%s' % fname)&
rootDir = 'd:\\assa'
for dirName, subdirList, fileList in os.walk(rootDir):
&print('Folder: %s' % dirName)
&for fname in fileList:
&&print('\t%s' % fname)目录结构为:
&&&&&&& 2.txt&
&&&&&&& 3.txt&
&&& 1.txt&
&1.txt输出结果为:
Folder: d:\Python Code\test&
&&& 1.txt&
Folder: d:\Python Code\test\f1&
&&& 2.txt&
Folder: d:\Python Code\test\f2&
&&& 3.txt&
Folder: d:\Python Code\test
Folder: d:\Python Code\test\f1
Folder: d:\Python Code\test\f2
&3.txt可以看到这是自顶向下的遍历顺序,如果我们要自底向上,先从最深处的文件开始遍历,可以加上topdown参数:[python] view plaincopyprint?import os&
rootDir = 'd:\\Python Code\\test'&
for dirName, subdirList, fileList in os.walk(rootDir, topdown = False):&
&&& print('Folder: %s' % dirName)&
&&& for fname in fileList:&
&&&&&&& print('\t%s' % fname)&
rootDir = 'd:\\Python Code\\test'
for dirName, subdirList, fileList in os.walk(rootDir, topdown = False):
&print('Folder: %s' % dirName)
&for fname in fileList:
&&print('\t%s' % fname)结果:
Folder: d:\Python Code\test\f1&
&&& 2.txt&
Folder: d:\Python Code\test\f2&
&&& 3.txt&
Folder: d:\Python Code\test&
&&& 1.txt&
Folder: d:\Python Code\test\f1
Folder: d:\Python Code\test\f2
Folder: d:\Python Code\test
&1.txt如果想要在搜索的时候加上条件?比如跳过第一个文件夹,os.walk也可以做到:[python] view plaincopyprint?import os&
rootDir = 'd:\\Python Code\\test'&
for dirName, subdirList, fileList in os.walk(rootDir):&
&&& print('Folder: %s' % dirName)&
&&& for fname in fileList:&
&&&&&&& print('\t%s' % fname)&
&&& if len(subdirList) & 0:&
&&&&&&& del subdirList[0]&
rootDir = 'd:\\Python Code\\test'
for dirName, subdirList, fileList in os.walk(rootDir):
&print('Folder: %s' % dirName)
&for fname in fileList:
&&print('\t%s' % fname)
&if len(subdirList) & 0:
&&del subdirList[0]结果:
Folder: d:\Python Code\test&
&&& 1.txt&
Folder: d:\Python Code\test\f2&
&&& 3.txt&
Folder: d:\Python Code\test
Folder: d:\Python Code\test\f2
&3.txt有的同学却不能得到正确的结果,我们可以看看如下代码:
import os&
rootDir = 'd:\\Python Code\\test'&
for dirName, subdirList, fileList in os.walk(rootDir):&
&&& print('Folder: %s' % dirName)&
&&& for fname in fileList:&
&&&&&&& print('\t%s' % fname)&
&&& if len(subdirList) & 0:&
&&&&&&& subdirList = subdirList[1:]&
rootDir = 'd:\\Python Code\\test'
for dirName, subdirList, fileList in os.walk(rootDir):
&print('Folder: %s' % dirName)
&for fname in fileList:
&&print('\t%s' % fname)
&if len(subdirList) & 0:
&&subdirList = subdirList[1:]
Folder: d:\Python Code\test&
&&& 1.txt&
Folder: d:\Python Code\test\f1&
&&& 2.txt&
Folder: d:\Python Code\test\f2&
&&& 3.txt&
Folder: d:\Python Code\test
Folder: d:\Python Code\test\f1
Folder: d:\Python Code\test\f2
看起来和之前的版本相似,但是却不能得到期望的结果。这是因为我们没有就地改变subdirList的值:
&& a=[1,2,3]&
&&& id(a)&
&&& del a[0]&
&&& id(a)&
&&& a = a[1:]&
&&& id(a)&
&&& a=[1,2,3]
&&& del a[0]
&&& a = a[1:]Python(14)
函数声明:os.walk(top, topdown = True, onerror = None)
参数说明:
参数top表示需要遍历的顶级目录的路径;
参数topdown的默认值是“True”表示首先返回顶级目录下的文件,然后再遍历子目录中的文件;当topdown的值为”False”时,表示先遍历子目录中的文件,然后再返回顶级目录下的文件;
参数onerror默认值为”None”,表示忽略文件遍历时的错误,如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。
返回值:函数返回一个元组,含有三个元素。这三个元素分别是:每次遍历的路径名、路径下子目录列表、目录下文件列表。
os.walk使用实例:
删除某个文件夹(当然可以通过os.listdir的递归调用删除)
def Remove_dir(top_dir):
if os.path.exists(top_dir) == False:
print "not exists"
if os.path.isdir(top_dir) == False:
print "not a dir"
for dir_path, subpaths, files in os.walk(top_dir, False):
for file in files:
file_path = os.path.join(dir_path, file)
print "delete file:%s" % file_path
os.remove(file_path)
print "delete dir:%s" % dir_path
os.rmdir(dir_path)
Remove_dir(r"C:\Users\Administrator\Desktop\zrbuN7zRuc")
os.path.walk()
函数声明:os.path.walk(top,func,arg)
参数说明:
参数top表示需要遍历的目录路径;
参数func表示回调函数,即对遍历路径进行处理的函数
所谓回调函数,是作为某个函数的参数使用,当某个时间触发时,程序将调用定义好的回调函数处理某个任务
##注意:walk的回调函数必须提供三个参数:第1个参数为os.path.walk的参数arg,第2个参数表示目录dirname,第3个参数表示文件列表names
##注意:os.path.walk的回调函数中的文件列表不和os.walk()那样将子目录和文件分开,而是混为了一摊,需要在回调函数中判断是文件还是子目录;
参数arg是传递给回调函数的元组,为回调函数提供处理参数,arg可以为空。回调函数的第1个参数就是用来接收这个传入的元组的。
过程说明:
以top 为根的目录树中的每一个目录 (包含 top 自身,如果它是一个目录),以参数 (arg, dirname, names)调用回调函数 funct。参数 dirname 指定访问的目录,参数 names 列出在目录中的文件(从 os.listdir(dirname)中得到)。回调函数可以修改 names 改变 dirname 下面访问的目录的设置,例如,避免访问树的某一部分。(由 names 关连的对象必须在合适的位置被修改,使用 del 或 slice 指派。) 注意:符号连接到目录不被作为一个子目录处理,并且因此 walk()将不访问它们。访问连接的目录你必须以os.path.islink(file) 和 os.path.isdir(file)标识它们,并且必须调用walk() 。
os.path.walk使用实例:
遍历文件夹下所有文件(os.path.walk()不能用于删除文件夹(可能是我没想到),因为os.path.walk()先遍历顶级目录,再遍历子目录中的文件)
def find_file(arg, dirname, files):
for file in files:
file_path = os.path.join(dirname, file)
if os.path.isfile(file_path):
print "find file:%s" % file_path
os.path.walk(r"C:\Users\Administrator\Desktop\4", find_file, ())
os.path.walk()与os.walk()产生的文件名列表并不相同:
os.walk()产生目录树下的目录路径和文件路径,而os.path.walk()只产生文件路径(是子目录与文件的混合列表)
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9805次
排名:千里之外
原创:14篇
转载:18篇
(1)(3)(3)(1)(3)(3)(9)(10)python使用os模块的os.walk遍历文件夹示例
代码如下:#-*- coding:utf-8 -*-
if __name__ == '__main__':&&& try:&&& '''traval and list all files and all dirs''' &&& for root, dirs, files in os.walk('D:' + os.sep + 'Python27'):&&&&&&& print '-------------------directory & ' + root + ' & --------------------------'
&&&&&&& for d in dirs:&&&&&&& print d&&&&&&& for f in files:&&&&&&& print f&&& except OSError, e:&&& print os.strerror(e.errno)
顶一下(0) 踩一下(0)
热门标签:

我要回帖

更多关于 python中os.walk 的文章

 

随机推荐