Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Read Text File into List in Python

  • 22 Apr, 2025
  • Com 1
Reading a file into a list in Python Efficiently

When reading a file into a Python list, always use the with open(…) context manager to open the file in read mode, and then use the readlines() function, which returns a list of lines with trailing newlines.

Reading a Text File into List in Python

To strip those newlines after each list element, you should use list comprehension and the rstrip() method.

For a more efficient and robust approach, pass the file object directly to the list() constructor, which will return a list of lines without trailing newlines in each element.

Here is a demo apple.txt file that we will use for read:

demo text file

Ensure the text file (apple.txt) is in the same directory as your Python script, or provide the full path to the file.

with open("apple.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()
    stripped_lines = [line.rstrip("\n") for line in lines]

print(stripped_lines)

# Output: ['apple', 'microsoft', 'amazon', 'alphabet', 'facebook']

And we get each line of the text file as an individual list element.

If possible, always specify encoding=’utf-8′ (or other) to prevent any Unicode issues.

We can improve the above code and add a try/except mechanism to handle the potential errors while performing this task.

import sys

try:
    with open("apple.txt", "r", encoding="utf-8") as f:
        lines = f.readlines()
        stripped_lines = [line.rstrip("\n") for line in lines]
except FileNotFoundError as e:
    print(f"Error: file not found: {e.filename}", file=sys.stderr)
    stripped_lines = []
except UnicodeDecodeError as e:
    print(
        f"Error: could not decode file (encoding issue) at byte {e.start}", file=sys.stderr)
    stripped_lines = []
except Exception as e:
    print(f"Unexpected error reading file: {e}", file=sys.stderr)
    stripped_lines = []
else:
    # Only runs if no exception was raised
    print(stripped_lines)
    
# Output: ['apple', 'microsoft', 'amazon', 'alphabet', 'facebook']

If you forget to open the wrong file, a misnamed file, or a file that does not exist, it will print the appropriate error and prevent the program from crashing.

Let’s use the list() constructor and see if we get the same output:

with open("apple.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()
    stripped_lines = [line.rstrip("\n") for line in lines]

print(stripped_lines)

# Output: ['apple', 'microsoft', 'amazon', 'alphabet', 'facebook']

Using pathlib (Modern File Handling)

If you’re using Python 3.4 or later, you should definitely use the pathlib module. It has simplified syntax and cross-platform paths.

With the help of .read_text() and .splitlines() method of the pathlib module, we can read a file, split it into individual lines, make a list out of it, and strip the newline from those individual elements.

from pathlib import Path

lines = Path('apple.txt').read_text(encoding='utf-8').splitlines()

companies_list = [line.strip() for line in lines if line.strip()]

print(companies_list)

# Output: ['apple', 'microsoft', 'amazon', 'alphabet', 'facebook']

Using read() and splitlines()

You can read an entire file as a big string into memory, then split on line boundaries without keeping “\n” using file.read() and .splitlines() methods.

from pathlib import Path

with open("apple.txt", "r", encoding="utf-8") as f:
    lines = f.read().splitlines()

print(lines)

# Output: ['apple', 'microsoft', 'amazon', 'alphabet', 'facebook']

You can see from the output that we don’t have to strip the newline character manually, as it is handled automatically.

For small files, this approach fits perfectly. However, this is not an efficient approach because if the file is very big, it will exhaust the memory.

Using read() and split()

In this approach, we first open a file in read mode and store its contents in memory. Then, we replace the “\n” with an empty space using the .replace() method, split the string at the full stop, and print the content.

with open('apple.txt') as f:
    data = f.read()
    removing_newline = data.replace('\n', ' ')
    converted_list = removing_newline.split(".")

print(converted_list)

# Output: ['apple microsoft amazon alphabet facebook']

The above output shows that the whole content of the file will be taken as a single element of the list. It does not split it into individual elements.

Handling extremely large files

If your file is very large, use line‑by‑line streaming iteration to minimize overload. It means we can load minimal lines into memory at a time.

lines = []

with open('apple.txt') as f:
    for line in f:
        lines.append(line.strip())  # Process line-by-line

print(lines)

# Output: ['apple', 'microsoft', 'amazon', 'alphabet', 'facebook']

Generator expression (one‑liner)

When you want to pass a “list‑like” object into APIs that accept iterables, you can use generator expressions.

lines = []

lines = (line.rstrip("\n") for line in open("apple.txt", encoding="utf-8"))

for ln in lines:
    print(ln)

# Output:
# apple
# microsoft
# amazon
# alphabet
# facebook

That’s all!

Post Views: 14
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Creating a Filename Containing Date or Time in Python
How to Create File If It Does Not Exist in Python

1 Comment

  1. Coolhappyasad

    September 13, 2022 at 8:02 am

    Thanks a lot this really help

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend