如何在python中递归地生成目录大小,比如du。呢? [英] How to generate directory size recursively in python, like du . does?
问题描述
可以说我的结构是这样的
/ - 在这里
/ one / some / dir
/ two
/ three / has / many / leaves
/ hello / world
$ b $
说/ one / some / dir包含一个500mb大文件,/ three / has / many / >我想为每个目录生成大小,以获得这个输出
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ one / some / dir 500mb
/ two 0
/ three / has / many / leaved - 400mb
/ three / has / many 800
/ three / has / 800 + someotherbigfilehere
我会如何去做这件事?
查看 os.walk
。具体来说,这个文档有一个例子来查找一个目录的大小:
import os $ b $ os from os.path import加入,取得
的根,dirs,os.walk('python / Lib / email')中的文件:
print root,consumes,
print sum(getsize(join ,
打印bytes in,len(files),非目录文件
如果'CVS'在dirs中:
dirs.remove( 'CVS')#不要访问CVS目录
这应该很容易修改你的目的。
$ p
这是一个未经测试的版本, pre> 从os.path导入os
导入join,getsize
dirs_dict = {}
我们需要从自下而上,以便一个目录可以轻松
#访问其子目录的大小。
为os.walk('python / Lib / email',topdown = False)中的root,dirs,files,
#循环遍历这个目录中的每个非目录文件,
size = sum(getsize(join(root,name))为文件中的名字)
#查看所有的子目录,并从`dirs_dict`中加上它们的大小
subdir_size = sum(dirs_dict [join(root,d)] for d in dirs)
#将这个目录的大小(加上子目录)存储在一个字典中,所以我们
#可以访问它后来
my_size = dirs_dict [root] = size + subdir_size
print'%s:%d'%(root,my_size)
Lets say my structure is like this
/-- am here
/one/some/dir
/two
/three/has/many/leaves
/hello/world
and say /one/some/dir contains a big file, 500mb, and /three/has/many/leaves contains a 400mb file in each folder.
I want to generate the size for each directory, to have this output
/ - in total for all
/one/some/dir 500mb
/two 0
/three/has/many/leaved - 400mb
/three/has/many 800
/three/has/ 800+someotherbigfilehere
How would I go about this?
Have a look at os.walk
. Specifically, the documentation has an example to find the size of a directory:
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
This should be easy enough to modify for your purposes.
Here's an untested version in response to your comment:
import os
from os.path import join, getsize
dirs_dict = {}
#We need to walk the tree from the bottom up so that a directory can have easy
# access to the size of its subdirectories.
for root, dirs, files in os.walk('python/Lib/email',topdown = False):
# Loop through every non directory file in this directory and sum their sizes
size = sum(getsize(join(root, name)) for name in files)
# Look at all of the subdirectories and add up their sizes from the `dirs_dict`
subdir_size = sum(dirs_dict[join(root,d)] for d in dirs)
# store the size of this directory (plus subdirectories) in a dict so we
# can access it later
my_size = dirs_dict[root] = size + subdir_size
print '%s: %d'%(root,my_size)
这篇关于如何在python中递归地生成目录大小,比如du。呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!