[英]Python fastest way to read a large number of small files into memory?
我正在嘗試讀取存儲在磁盤上的幾千個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”構造確保為您服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.