给定一段代码:
from glob import glob, iglob
for fn in glob('/*'):
print fn
print ''
for fn in iglob('/*'):
print fn
阅读documentation对于 glob,我看到 glob() 返回文件的基本列表和 iglob 一个迭代器。但是,我可以对两者进行迭代,并且它们每个都返回相同的文件列表。
我已阅读有关 Iterator 的文档但它并没有真正阐明这个主题!
那么返回 Iterator 的 iglob() 对 glob() 的列表有什么好处?我是否比我的老 friend 获得了额外的功能?
最佳答案
文档本身中提到了差异:
Return an iterator which yields the same values as glob() without actually storing them all simultaneously.
基本上列表将包含内存中的所有项目。迭代器不需要,因此它需要更少的内存。
关于Python 迭代器 : What does iglob( )'s Iterator provide over glob()' s list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4287162/