How can I open multiple files using “with open” in Python?

后端 未结 7 930
天涯浪人
天涯浪人 2020-11-22 08:15

I want to change a couple of files at one time, iff I can write to all of them. I\'m wondering if I somehow can combine the multiple open calls with the

7条回答
  •  一向
    一向 (楼主)
    2020-11-22 08:51

    Since Python 3.3, you can use the class ExitStack from the contextlib module to safely
    open an arbitrary number of files.

    It can manage a dynamic number of context-aware objects, which means that it will prove especially useful if you don't know how many files you are going to handle.

    In fact, the canonical use-case that is mentioned in the documentation is managing a dynamic number of files.

    with ExitStack() as stack:
        files = [stack.enter_context(open(fname)) for fname in filenames]
        # All opened files will automatically be closed at the end of
        # the with statement, even if attempts to open files later
        # in the list raise an exception
    

    If you are interested in the details, here is a generic example in order to explain how ExitStack operates:

    from contextlib import ExitStack
    
    class X:
        num = 1
    
        def __init__(self):
            self.num = X.num
            X.num += 1
    
        def __repr__(self):
            cls = type(self)
            return '{cls.__name__}{self.num}'.format(cls=cls, self=self)
    
        def __enter__(self):
            print('enter {!r}'.format(self))
            return self.num
    
        def __exit__(self, exc_type, exc_value, traceback):
            print('exit {!r}'.format(self))
            return True
    
    xs = [X() for _ in range(3)]
    
    with ExitStack() as stack:
        print(len(stack._exit_callbacks)) # number of callbacks called on exit
        nums = [stack.enter_context(x) for x in xs]
        print(len(stack._exit_callbacks))
    
    print(len(stack._exit_callbacks))
    print(nums)
    

    Output:

    0
    enter X1
    enter X2
    enter X3
    3
    exit X3
    exit X2
    exit X1
    0
    [1, 2, 3]
    

提交回复
热议问题