with open
语句打开文件,因为我需要一起打开几百个文件并使用k-way Merge合并它们。因此,在这一点上,我需要非常快速的I/O,它不会将整个/大部分文件存储在内存中(因为有数百个文件,每个文件中的每个文件)。我只需要一次阅读一行以进行K-Way合并。减少内存使用是我目前的主要重点。 我了解到
with open
open
语句中一起
with open
一起
contextmanager
with
语句上下文管理器的出厂功能”。例如:
contextlib.ContextDecorator
如果您不提前知道所有文件,那么创建一个支持它们在上下文中逐步添加它们的上下文管理器同样容易。在下面的代码中,AMultiFileManager
类的实现。
contextlib.ExitStack
尽管不是2.7的解决方案,但我应该注意到3.3+的一个好的,正确的解决方案,可用于执行此操作。很好:from contextlib import ExitStack
with open('source_dataset.txt') as src_file, ExitStack() as stack:
files = [stack.enter_context(open(fname, 'w')) for fname in fname_list]
... do stuff with src_file and the values in files ...
... src_file and all elements in stack cleaned up on block exit ...
在此处被置于此问题,因为这个问题被标记为未指定Python版本的duplate的“原始”。
open
大约翻译为
open
在您的情况下,我不会使用
with open(...) as f:
# do stuff
语法。如果您有文件名列表,请执行此类操作。
f = open(...)
# do stuff
f.close()
如果您真的想使用with open
语法,则可以制作自己的上下文管理器,以接受文件名列表
filenames = os.listdir(file_directory)
open_files = map(open, filenames)
# do stuff
for f in open_files:
f.close()
with open
在这种情况下,我看到使用上下文管理器的唯一优势是您不能忘记关闭文件。但是手动关闭文件没有错。请记住,当您的程序退出时,操作系统将收回其资源。