Python --Most elegant way to read lines of file into list

Ref: [1] - Python: read file line by line into array

For most cases, to read lines of file to a list, we just use

with open(fileName) as f:
  lineList = f.readlines()

In this case, every element in the list contain a \n in the end the string, which would be extremely annoying in some cases.

And there will be same problem if you use:

lineList = list()
with open(fileName) as f:
  for line in f:
    lineList.append(line)

To overcome this, you could use:

lineList = [line.rstrip('\n') for line in open(fileName)]

Recommended Posts

Python --Most elegant way to read lines of file into list
Template of python script to read the contents of the file
Summary of how to use Python list
[Python] How to read a csv file (read_csv method of pandas module)
How to read a CSV file with Python 2/3
Read a file containing garbled lines in Python
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Python] How to read excel file with pandas
List of Python code to move and remember
Various ways to read the last line of a csv file in Python
[Python] Scan the inside of the folder including subfolders → Export the file list to CSV
List of python modules
Easy way to check the source of Python modules
List of posts related to optimization by Python to docker
How to write a list / dictionary type of Python3
Read the xml file by referring to the Python tutorial
Read Python csv file
Try to get the function list of Python> os package
[Python] How to make a list of character strings character by character
How to switch the configuration file to be read by Python
How to shuffle a part of a Python list (at random.shuffle)
[Markov chain] I tried to read negative emotions into Python.
[Markov chain] I tried to read a quote into Python.
How to get a list of built-in exceptions in python
Python> Read from a multi-line string instead of a file> io.StringIO ()
How to read an Excel file (.xlsx) with Pandas [Python]
I tried to divide the file into folders with Python
List of libraries to install when installing Python using Pyenv
[Python3] List of sites that I referred to when I started Python
Command to list all files in order of file name
How to connect the contents of a list into a string
Summary of python file operations
Summary of Python3 list operations
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
[Python] Another way to import
[Python] Copy of multidimensional list
[Introduction to Python] <list> [edit: 2020/02/22]
Try to get a list of breaking news threads in Python.
I tried to create a list of prime numbers with python
[Python] A program that rotates the contents of the list to the left
Easy way to customize Python import
Easy encryption of file contents (Python)
[Python] Write to csv file with Python
Output to csv file with Python
Convert list to DataFrame with python
[Python beginner] Divide one list (5 lines).
Python> list> Convert double list to single list
[Python] How to use list 3 Added
About the basics list of Python basics
File upload to Azure Storage (Python)
[Python] How to put any number of standard inputs in a list
Python> sys.path> List of strings indicating the path to search for modules
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
Read CSV file with Python and convert it to DataFrame as it is
[Markov chain] I tried to read quotes and negative emotions into Python.
Python script that outputs all records of Oracle table to CSV file
How to format a list of dictionaries (or instances) well in Python
I made a program to check the size of a file in Python