python3中os模块_python3-os模块中的os.walk(目录树生成器)

#先看源码

def walk(top, topdown=True, οnerrοr=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. No matter

the value of topdown, the list of subdirectories is retrieved before the

tuples for the directory and its subdirectories are generated.

By default errors from the os.scandir() call are ignored. If

optional arg 'onerror' is specified, it should be a function; it

will be called with one argument, an OSError 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.

原文注释大概这么些意思:

这是一个目录树的生成器。对于位于顶部的目录树中的每个目录产生一个3元元组。(dirpath,dirnames,filenames)

第一个为起始路径,第二个为起始路径下的所有文件夹的名字,第三个是起始路径下的所有非目录文件。

dirpath 是一个string,代表目录的路径,

dirnames 是一个list,包含了dirpath下所有子目录的名字。

filenames 是一个list,包含了非目录文件的名字。

注意:这些目录和文件名字不包含路径信息,如果需要得到全路径,需要使用os.path.join(dirpath, name).,通过for循环自动完成递归枚举

a797c6c3649d5ab3ac38d7cc31483f99.png

如果可选的参数 'topdown'为真或未指定,则(目录是自上而下生成的)。

cf3bab0fcb8f18ba55e67e8f312a61ce.png

如果topdown为false,则子目录(目录自下而上地生成)。

b_0_201806210430551199.jpg

当topdown为true时,调用者可以就地修改dirnames列表,(例如,通过del或slice赋值),并且walk只会递归到

子目录,其名称保留在dirname中;这可以用来修剪搜索,或强制执行特定的访问命令。

修改目录名的时候topdown为false是无效的,因为在dirname中有目录已经生成的时间dirname本身是生成的。

不管topdown的值,子目录列表在生成目录及其子目录的元组。

参数onerror的默认值是"None",表示忽略文件遍历时产生的错误.如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历

总结:

os.walk(top[,topdown=True[,οnerrοr=None[,followlinks=False]]])

top -- 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames, filenames)【文件夹路径, 文件夹名字, 文件名】。

topdown --可选,为True或者没有指定, 一个目录的的3-元组将比它的任何子文件夹的3-元组先产生 (目录自上而下)。如果topdown为 False, 一个目录的3-元组将比它的任何子文件夹的3-元组后产生 (目录自下而上)。

onerror -- 可选,是一个函数; 它调用时有一个参数, 一个OSError实例。报告这错误后,继续walk,或者抛出exception终止walk。

followlinks -- 设置为 true,则通过软链接访问目录。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值