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

Posted on

Solving problem is about exposing yourself to as many situations as possible like How can I open multiple files using “with open” in Python? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How can I open multiple files using “with open” in Python?, which can be followed any time. Take easy to follow this discuss.

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

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 with statement:

  with open('a', 'w') as a and open('b', 'w') as b:
except IOError as e:
  print 'Operation failed: %s' % e.strerror

If that’s not possible, what would an elegant solution to this problem look like?

Answer #1:

As of Python 2.7 (or 3.1 respectively) you can write

with open('a', 'w') as a, open('b', 'w') as b:

In earlier versions of Python, you can sometimes use
contextlib.nested() to nest context managers. This won’t work as expected for opening multiples files, though — see the linked documentation for details.

In the rare case that you want to open a variable number of files all at the same time, you can use contextlib.ExitStack, starting from Python version 3.3:

with ExitStack() as stack:
    files = [stack.enter_context(open(fname)) for fname in filenames]
    # Do something with "files"

Most of the time you have a variable set of files, you likely want to open them one after the other, though.

Answered By: Sven Marnach

Answer #2:

Just replace and with , and you’re done:

    with open('a', 'w') as a, open('b', 'w') as b:
except IOError as e:
    print 'Operation failed: %s' % e.strerror
Answered By: Michael

Answer #3:

For opening many files at once or for long file paths, it may be useful to break things up over multiple lines. From the Python Style Guide as suggested by @Sven Marnach in comments to another answer:

with open('/path/to/InFile.ext', 'r') as file_1,
     open('/path/to/OutFile.ext', 'w') as file_2:
Answered By: Michael Ohlrogge

Answer #4:

Nested with statements will do the same job, and in my opinion, are more straightforward to deal with.

Let’s say you have inFile.txt, and want to write it into two outFile’s simultaneously.

with open("inFile.txt", 'r') as fr:
    with open("outFile1.txt", 'w') as fw1:
        with open("outFile2.txt", 'w') as fw2:
            for line in fr.readlines():


I don’t understand the reason of the downvote. I tested my code before publishing my answer, and it works as desired: It writes to all of outFile’s, just as the question asks. No duplicate writing or failing to write. So I am really curious to know why my answer is considered to be wrong, suboptimal or anything like that.

Answered By: FatihAkici

Answer #5:

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]


enter X1
enter X2
enter X3
exit X3
exit X2
exit X1
[1, 2, 3]
Answered By: timgeb

Answer #6:

With python 2.6 It will not work, we have to use below way to open multiple files:

with open('a', 'w') as a:
    with open('b', 'w') as b:
Answered By: Aashutosh jha

Answer #7:

Late answer (8 yrs), but for someone looking to join multiple files into one, the following function may be of help:

def multi_open(_list):
    for x in _list:
            with open(x) as f:
            # print(f"Cannot open file {x}")
fl = ["C:/bdlog.txt", "C:/Jts/tws.vmoptions", "C:/not.exist"]

2018-10-23 19:18:11.361 PROFILE  [Stop Drivers] [1ms]
2018-10-23 19:18:11.361 PROFILE  [Parental uninit] [0ms]
# This file contains VM parameters for Trader Workstation.
# Each parameter should be defined in a separate line and the
Answered By: Pedro Lobito
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *