python3 yield 实现 os.walk 的功能
来源:互联网 发布:权力的游戏分析知乎 编辑:程序博客网 时间:2024/05/21 22:46
centos6.7 python-3.5.2
os.walk(dir) 函数生成一个generator
用for遍历可以访问dir文件夹下所有的文件或文件夹
#!/usr/bin/env python3import osfrom os.path import isdir,joindef conv(f): lf=os.listdir(f) #获取f目录下的一级子目录和文件 df=[i for i in lf if isdir(join(f,i))] #一级子目录 ff=list(set(lf)-set(df)) #f目录下的文件 return (f,df,ff) def walk(f): yield conv(f) for e in conv(f)[1]: tf=join(conv(f)[0],e) #给目录名加上前导路径 yield conv(tf) for a,b,c in walk(tf): #遍历子目录的文件和目录 for k in b: yield conv(join(a,k)) for i in walk('.'): print(i)
运行结果:
[willie@localhost .walker]$ python3 ../walk.py
('.', ['a', 'b'], ['d', 'c'])
('./a', ['ac'], ['aa', 'ab'])
('./a/ac', [], ['aca'])
('./b', [], [])
[willie@localhost .walker]$ python3
Python 3.5.2 (default, Dec 7 2016, 23:38:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> for i in os.walk('.'):
... print(i)
...
('.', ['a', 'b'], ['d', 'c'])
('./a', ['ac'], ['ab', 'aa'])
('./a/ac', [], ['aca'])
('./b', [], [])
百度得 os.walk 的官方源码为:
def walk(top, topdown=True, onerror=None, followlinks=False): """Directory tree generator. For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). If optional arg 'topdown' is true or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). If topdown is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up). When topdown is true, the caller can modify the dirnames list in-place (e.g., via del or slice assignment), and walk will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, or to impose a specific order of visiting. Modifying dirnames when topdown is false is ineffective, since the directories in dirnames have already been generated by the time dirnames itself is generated. By default errors from the os.listdir() call are ignored. If optional arg 'onerror' is specified, it should be a function; it will be called with one argument, an os.error instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object. By default, os.walk does not follow symbolic links to subdirectories on systems that support them. In order to get this functionality, set the optional argument 'followlinks' to true. Caution: if you pass a relative pathname for top, don't change the current working directory between resumptions of walk. walk never changes the current directory, and assumes that the client doesn't either. Example: import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print root, "consumes", print sum([getsize(join(root, name)) for name in files]), print "bytes in", len(files), "non-directory files" if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories """ islink, join, isdir = path.islink, path.join, path.isdir # We may not have read permission for top, in which case we can't # get a list of the files the directory contains. os.path.walk # always suppressed the exception then, rather than blow up for a # minor reason when (say) a thousand readable directories are still # left to visit. That logic is copied here. try: # Note that listdir and error are globals in this module due # to earlier import-*. names = listdir(top) except error, err: if onerror is not None: onerror(err) return dirs, nondirs = [], [] for name in names: if isdir(join(top, name)): dirs.append(name) else: nondirs.append(name) if topdown: yield top, dirs, nondirs for name in dirs: new_path = join(top, name) if followlinks or not islink(new_path): for x in walk(new_path, topdown, onerror, followlinks): yield x if not topdown: yield top, dirs, nondirs
0 0
- python3 yield 实现 os.walk 的功能
- Python3之os.walk()用法
- os.walk的用法
- python3 yield 的理解
- Python中os模块常用功能--os.walk()
- os.walk
- os.walk
- os.walk()
- os.walk()
- os.walk
- python3 yield
- Python os.walk的用法与举例
- python库os.walk的用法
- [第一弹]os.walk的相关用法
- Python的os.walk()方法详细讲解
- Python os.walk的用法与举例
- Python os.walk的用法与举例
- python中的os.walk函数的用法
- flume日志收集系统
- Sass学习之路(13)——字符串函数
- 唐都DAC0832产生梯形波并在虚拟仪器中的示波器中观察
- 【docker】使用docker快速搭建nginx+php开发环境
- Java程序员从笨鸟到菜鸟之(十三)java网络通信编程
- python3 yield 实现 os.walk 的功能
- Unit03: 类的定义与实例化 、 构造函数与初始化表 、 this指针与常函数
- ListView添加头布局事件处理
- angularJS1 报错 Error: [ng:areq] Error: [$controller:ctrlreg]
- ffmpeg 使用方法
- CSS3的部分常用新特性
- 内核3.x版本之后设备树机制
- Shredding Company(简单的搜索剪枝)
- Linux_4th_第1个linux驱动___内核与驱动模块的第一次接触