python3读取文件夹-python3获取文件及文件夹大小

Help on function walk inmodule os:

walk(top, topdown=True, οnerrοr=None, followlinks=False)

Directory tree generator.

For each directoryinthe directory tree rooted at top (including top

itself, but excluding"." and ".."), yields a 3-tuple

dirpath, dirnames, filenames

dirpathis a string, the path to the directory. dirnames isa list of

the names of the subdirectoriesin dirpath (excluding "." and "..").

filenamesis a list of the names of the non-directory files indirpath.

Note that the namesinthe lists are just names, with no path components.

To get a full path (which begins with top) to a fileor directory indirpath, do os.path.join(dirpath, name).

If optional arg"topdown" is true or not specified, the triple fora

directoryis generated before the triples forany of its subdirectories

(directories are generated top down). If topdownisfalse, the triplefor a directory is generated after the triples forall of its

subdirectories (directories are generated bottom up).

When topdownis true, the caller can modify the dirnames list in-place

(e.g., viadel or slice assignment), andwalk will only recurse into the

subdirectories whose names remainindirnames; this can be used to prune the

search,orto impose a specific order of visiting. Modifying dirnames when

topdownis false is ineffective, since the directories indirnames have

already been generated by the time dirnames itselfisgenerated. No matter

the value of topdown, the list of subdirectoriesisretrieved before the

tuplesfor the directory andits subdirectories are generated.

By default errorsfromthe os.scandir() call are ignored. If

optional arg"onerror" isspecified, it should be a function; it

will be called with one argument, an OSError instance. It can

report the error tocontinue with the walk, or raisethe exception

to abort the walk. Note that the filenameisavailable as the

filename attribute of the exception object.

By default, os.walk doesnotfollow 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:importosfrom os.path importjoin, getsizefor root, dirs, files in os.walk("python/Lib/email"):print(root, "consumes", end="")print(sum([getsize(join(root, name)) for name in files]), end="")print("bytes in", len(files), "non-directory files")if "CVS" indirs:

dirs.remove("CVS") #don"t visit CVS directories

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值