简体   繁体   English

Python glob.glob(dir)内存错误

[英]Python glob.glob(dir) Memory Error

I'm dealing with a memory issue when searching a folder with milions of files. 搜索包含数百万个文件的文件夹时,我正在处理内存问题。 Does anyone know how to overcome this situation? 有谁知道如何克服这种情况? Is there some way to put a limit of files that glob will search? 有什么方法可以限制glob搜索的文件吗? So it could be executed in chunks? 因此可以分块执行吗?

Traceback (most recent call last):
  File "./lb2_lmanager", line 533, in <module>
   main(sys.argv[1:])
 File "./lb2_lmanager", line 318, in main
    matched = match_files(policy.directory, policy.file_patterns)
  File "./lb2_lmanager", line 32, in wrapper
    res = func(*args, **kwargs)
  File "./lb2_lmanager", line 380, in match_files
    listing = glob.glob(directory)
  File "/usr/lib/python2.6/glob.py", line 16, in glob
    return list(iglob(pathname))
  File "/usr/lib/python2.6/glob.py", line 43, in iglob
    yield os.path.join(dirname, name)
  File "/usr/lib/python2.6/posixpath.py", line 70, in join
   path += '/' + b
MemoryError

Try using generators instead of lists . 尝试使用generators而不是lists
To understand what generators are read this 要了解什么发电机阅读

import glob
dir_list = glob.iglob(YOUR_DIRECTORY)
for file in dir_list:
    print file

Change YOUR_DIRECTORY to the directory that you would like to list. YOUR_DIRECTORY更改为您要列出的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM