第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python3读取文件夹-python3获取文件及文件夹大小

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

时间:2023-05-28 06:52:42

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。