Nono.MA

Read Text from a File with Python

JUNE 30, 2022

Here's how to read text from a file in Python; maybe a file that already exists or a file to which you wrote text with Python.

file = open('/your/file.txt', 'r')

# Read all file contents
contents = file.read()

# Read the lines of the file
lines = file.readlines()

# Iterate through the lines
for line in lines:
  print(line)

BlogCodePython