我使用以下函数从目标目录下获取系统中的所有文件大小。在
def get_files(target):
# Get file size and modified time for all files from the target directory and down.
# Initialize files list
filelist = []
# Walk the directory structure
for root, dirs, files in os.walk(target):
# Do not walk into directories that are mount points
dirs[:] = filter(lambda dir: not os.path.ismount(os.path.join(root, dir)), dirs)
for name in files:
# Construct absolute path for files
filename = os.path.join(root, name)
# Test the path to account for broken symlinks
if os.path.exists(filename):
# File size information in bytes
size = float(os.path.getsize(filename))
# Get the modified time of the file
mtime = os.path.getmtime(filename)
# Create a tuple of filename, size, and modified time
construct = filename, size, str(datetime.datetime.fromtimestamp(mtime))
# Add the tuple to the master filelist
filelist.append(construct)
return(filelist)
我如何修改它以包含包含目录和目录总大小的第二个列表?我尝试在一个函数中包含这个操作,希望比在单独的函数中执行第二次遍历来获取目录信息和大小更有效。在
这样做的目的是能够用前20个最大文件的排序列表和前10个最大目录的第二个排序列表进行报告。在
谢谢你们的建议。在
我将目录输出到字典而不是列表中,但请看您是否喜欢:
如果要将dirdict作为元组列表,只需执行以下操作:
^{pr2}$我有很多剧本,我只是上传了大文件.py'到githubhttp://github.com/sente/sys-utils/blob/master/bigfiles.py
它不计算总的累计目录大小,但它可以修改,没有太多的麻烦。在
我还有其他代码,让我们在给定深度上合计目录大小,例如:
FS只被爬网一次,但是树一旦被创建就需要被遍历以获得完整的大小,如果你从叶节点开始,一直到根节点,你可以最有效地计算每个dir的总大小。在
相关问题 更多 >
编程相关推荐