Read the file line by line in Python

It's a very basic thing, but I tried to find out how to read a text file that is python line by line.

The smartest way

with open('./test.txt') as f:
    for line in f:
       print line

https://journal.lampetty.net/entry/wp/418

There are many other things

Probably the above is the smartest. I've seen a number of samples that use read (), readline (), readlines (), etc., but I don't think I need to use them if I'm processing line by line.

read()use


with open('./test.txt') as f:
    lines = f.read()
    for l in lines.split("\n"):
        print(l)

readline()use


with open('./test.txt') as f:
    line = f.readline()
    while line:
        print(line.rstrip("\n"))
        line = f.readline()

readlines()use


with open('./test.txt') as f:
    lines = f.readlines()
    for l in lines:
        print(l.rstrip("\n"))

Python has too many solutions to what you want to do, so it's not good to spend a lot of time thinking about which one is better. ..

Recommended Posts

Read the file line by line in Python
Read the file line by line in Python
[Python] Read the specified line in the file
Download the file in Python
Read the standard output of a subprocess line by line in Python
Read line by line from a file with Python
[Implementation example] Read the file line by line with Cython (Python) from the last line
Read the xml file by referring to the Python tutorial
Save the binary file in Python
Various ways to read the last line of a csv file in Python
[Python] Open the csv file in the folder specified by pandas
How to switch the configuration file to be read by Python
Google search for the last line of the file in Python
Let's read the RINEX file with Python ①
Read the file by specifying the character code.
Sort the file names obtained by Python glob in numerical order
Read a file in Python with a relative path from the program
File operations in Python
Read DXF in python
File processing in Python
File operations in Python
Read Python csv file
Read a file containing garbled lines in Python
Read table data in PDF file with Python
A memo organized by renaming the file names in the folder with python
Read the file with python and delete the line breaks [Notes on reading the file]
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
Download the file while viewing the progress in Python 3.x
Template of python script to read the contents of the file
Fizzbuzz in Python (in one line)
Find the difference in Python
How to read all the classes contained in * .py in the directory specified by Python
Python --Read data from a numeric data file and find the multiple regression line.
Read Euler's formula in Python
Read the csv file and display it in the browser
Fourier transform the wav file read by Python, reverse transform it, and write it again.
Try LINE Notify in Python
Read Namespace-specified XML in Python
Shift the alphabet string by N characters in Python
Store the stock price scraped by Python in the DB
I want to get the file name, line number, and function name in Python 3.4
Read Fortran output in python
Sort by date in python
Change the standard output destination to a file in Python
[Python] Read the csv file and display the figure with matplotlib
Get the MIME type in Python and determine the file format
[Note] Import of a file in the parent directory in Python
After enabling the python virtual environment in the batch file, run the python file
Visualize the correlation matrix by principal component analysis in Python
[Python] Read command line arguments from file name or stdin
Extract the xz file with python
Getting the arXiv API in Python
Python in the browser: Brython's recommendation
Hit the Sesami API in Python
Try the Python LINE Pay SDK
Linebot creation & file sharing in Python
Read PNG chunks in Python (class)
In the python command python points to python3.8
Implement the Singleton pattern in Python
Create a binary file in Python
Have python read the command output