gpt4 book ai didi

Python将大量小文件读入内存的最快方法?

转载 作者:行者123 更新时间:2023-11-28 18:40:13 25 4
gpt4 key购买 nike

我正在尝试读取存储在磁盘上的数千个 html 文件。

有什么办法比;

for files in os.listdir('.'):
if files.endswith('.html') :
with (open) files as f:
a=f.read()
#do more stuffs

最佳答案

对于类似的问题,我使用了这段简单的代码:

import glob
for file in glob.iglob("*.html"):
with open(file) as f:
a = f.read()

iglob不会同时存储所有文件,这对于一个巨大的目录来说是完美的。
请记住在完成后关闭文件,构造 "with-open"为您确定。

关于Python将大量小文件读入内存的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202199/

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