Read the file line by line in Python

Read line by line from file with Python -Qiita

It seems better to use the with statement to read the file.

def main():
    import inspect
    filename = inspect.currentframe().f_code.co_filename

    with open(filename) as fh:
        for line in fh:
            print(line.strip())


if __name__ == '__main__':
    main()
    exit(0)

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
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 ()
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 Outlook emails 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
[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
Make python segfault in one line
Extract the xz file with python
Getting the arXiv API in Python
File / folder path manipulation in Python
Read Protocol Buffers data in Python3
Hit the Sesami API in Python
Try the Python LINE Pay SDK
Linebot creation & file sharing in Python
Get the desktop path in Python
Read PNG chunks in Python (class)
Get the script path in Python
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