How to “with open” a list of files and obtain their handles in python? [duplicate]

↘锁芯ラ 提交于 2019-12-24 08:39:30

问题


Is it possible to with open() all files contained in a list and create file handles for writing?

For example, if my function accepts a list of filenames for data-splitting in a machine learning task,

fname_list = ['train_dataset.txt', 'validate_dataset.txt', 'test_dataset.txt']

then it would be convenient to be able to do:

with open('source_dataset.txt) as src_file, open(name_list, 'w') as <DONT_KNOW_WHAT_TO_DO_HERE>:

And perform some data splitting within the block.

Edit: So my question is basically "Is it possible to obtain multiple file handles for a list of files opened with 'with open()'?"


回答1:


In Python 3.3 and higher, contextlib.ExitStack can be used to do this correctly and nicely:

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 ...



回答2:


You can define a class openfiles to support the with statement:

class openfiles:
    def __init__(self, filelist, mode='r'):
        self.fhandles = [open(f, mode) for f in filelist]

    def __enter__(self):
        return self.fhandles

    def __exit__(self, type, value, traceback):
        map(file.close, self.fhandles)

Then you can:

with openfiles(['file1', 'file2']) as files:
    for f in files:
        print(f.read())


来源:https://stackoverflow.com/questions/39782888/how-to-with-open-a-list-of-files-and-obtain-their-handles-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!