[Python] How to open two or more files at the same time

You may want to open two or more files at the same time in Python. For example, you might want to read one file, do some processing, and write the result to another file.

If you write a program obediently,

with open('./output.txt', mode='w') as fw:
    with open('./input.txt', mode='r') as fi:
        for line in fi:
            i = int(line.strip())
            i += 1 #Processing just by adding 1
            fw.write(str(i) + '\n')

I think it will be.

ʻInput.txt`

1
2
3

If so, ʻoutput.txt` is

2
3
4

It will be.

However, the above program is not smart because the with clause is nested and the indentation is deep. Actually, in this program, you can write the with clauses together as follows.

with open('./input.txt', mode='r') as fi, open('./output.txt', mode='w') as fw:
    for line in fi:
        i = int(line.strip())
        i += 1 #Processing just by adding 1
        fw.write(str(i) + '\n')

I think the indentation has become shallower and a little (?) Smarter.

Recommended Posts

[Python] How to open two or more files at the same time
How to get the files in the [Python] folder
How to get a list of files in the same directory with python
How to measure processing time in Python or Java
How to get the Python version
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
How to display bytes in the same way in Java and Python
Put the process to sleep for a certain period of time (seconds) or more in Python
How to set the server time to Japanese time
Call the python debugger at any time
Python open and io.open are the same
How to get the date and time difference in seconds with python
Count the number of times two values appear in a Python 3 iterator type element at the same time
How to use the C library in Python
How to create large files at high speed
How to use MkDocs for the first time
[Python] How to use two types of type ()
How to open a web browser from python
Summary of how to import files in Python 3
I want to make a music player and file music at the same time
[Python] How to save images on the Web at once with Beautiful Soup
[Algorithm x Python] How to use the list
How to erase the characters output by Python
How to measure execution time with Python Part 1
Turn multiple lists with a for statement at the same time in Python
How to measure execution time with Python Part 2
How to execute a schedule by specifying the Python time zone and execution frequency
[Python] How to calculate the approximation formula of the same intercept 0 as Excel [scikit-learn] Memo
How to unit test a function containing the current time using freezegun in python
[Note] How to write QR code and description in the same image with python
When the variable you want to superscript with matplotlib is two or more characters
[Python] How to remove duplicate values from the list
How to retrieve the nth largest value in Python
How to get the variable name itself in python
Think about how to program Python on the iPad
[Introduction to Python] How to iterate with the range function?
[Reintroduction to python] How to import via the parent directory
[Python] How to specify the download location with youtube-dl
Visualize data and understand correlation at the same time
How to measure mp3 file playback time with python
[Python] How to use the graph creation library Altair
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
[Python] How to split and modularize files (simple, example)
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] Summary of how to specify the color of the figure
How to use the model learned in Lobe in Python
[Introduction to Python] How to stop the loop using break?
[Python] How to rewrite the table style with python-pptx [python-pptx]
How to enjoy Python on Android !! Programming on the go !!
[Python] How to output the list values in order
How to install Python
How to install python
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
How to save memory when reading huge XML of several GB or more in Python
How to write offline real time I tried to solve the problem of F02 with Python
Steps to change table and column names in your Django model at the same time
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
How to calculate the sum or average of time series csv data in an instant
Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time