Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open multiple files using "with open" in Python?

Tags:

python

file-io

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:

try:
  with open('a', 'w') as a and open('b', 'w') as b:
    do_something()
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?

like image 540
Frantischeck003 Avatar asked Jan 06 '11 16:01

Frantischeck003


3 Answers

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

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

(Historical note: 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"

Note that more commonly you want to process files sequentially rather than opening all of them at the same time, in particular if you have a variable number of files:

for fname in filenames:
    with open(fname) as f:
        # Process f
like image 117
Sven Marnach Avatar answered Nov 03 '22 20:11

Sven Marnach


Just replace and with , and you're done:

try:
    with open('a', 'w') as a, open('b', 'w') as b:
        do_something()
except IOError as e:
    print 'Operation failed: %s' % e.strerror
like image 124
Michael Avatar answered Nov 03 '22 21:11

Michael


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:
    file_2.write(file_1.read())
like image 88
Michael Ohlrogge Avatar answered Nov 03 '22 19:11

Michael Ohlrogge