gpt4 book ai didi

大数据 os.path.getsize 上的 Python 代码性能

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:03 27 4
gpt4 key购买 nike

下面是我按升序获取文件大小的代码。

def Create_Files_Structure(directoryname):
for path, subdirs, files in os.walk(directoryname,followlinks=False):
subdirs[:] = [d for d in subdirs if not d[0] == '.']
try:
files_list.extend([(os.path.join(path, file),os.path.getsize(os.path.join(path, file))) for file in files ])
except Exception as e:
print()
files_list.sort(key=lambda s: s[1], reverse=True)
for pair in files_list:
print(pair)
print(len(files_list))

start=time.time()
Create_Files_Structure("/home/<username>")
end=time.time()
print(end-start)

此代码有效,但如果目录大小​​以 TB 或 PB 为单位,性能会很慢。请提出任何改进代码以获得更快结果的建议。

最佳答案

  1. 要感受一下您能获得多快,请尝试在目录上运行 du -k 并计时。对于完整列表,您可能不会比使用 Python 更快。
  2. 如果您在 Python < 3.5 上运行,请尝试升级或使用 scandir以获得不错的性能改进。
  3. 如果您真的不需要整个文件列表但可以接受最大的 1000 个文件:

避免保留列表并使用 heapq.nlargest带发电机

def get_sizes(root):
for path, dirs, files in os.walk(root):
dirs[:] = [d for d in dirs if not d.startswith('.')]
for file in files:
full_path = os.path.join(path, file)
try:
# keeping the size first means no need for a key function
# which can affect performance
yield (os.path.getsize(full_path), full_path)
except Exception:
pass

import heapq
for (size, name) in heapq.nlargest(1000, get_sizes(r"c:\some\path")):
print(name, size)

编辑 - 为了在 Windows 上变得更快 - os.scandir 生成已经包含有助于避免另一个系统调用的大小的条目。

这意味着使用 os.scandir 并自己递归,而不是依赖 os.walk,后者不会产生该信息。

scandir PEP 471 中有一个类似的工作示例 get_tree_size() 函数可以轻松修改以生成名称和大小。每个条目的大小都可以通过 entry.stat(follow_symlinks=False).st_size 访问。

关于大数据 os.path.getsize 上的 Python 代码性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47284485/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com