Menu Close

How to open multiple files using “with open” in Python?

Sometimes, we want to open multiple files using "with open" in Python.

In this article, we’ll look at how to open multiple files using "with open" in Python.

How to open multiple files using "with open" in Python?

To open multiple files using "with open" in Python, we can call open multiple times.

For instance, we write

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

to open files a and b with open and with.

Then we can do whatever we want with them inside the block.

Conclusion

To open multiple files using "with open" in Python, we can call open multiple times.

Posted in Python, Python Answers