banner

© 2004-19 ict4u.net

Reading a Text File line by Line

Use the following steps:
  1. open the file and assign a file handler
  2. Use a for loop to process each line of data in the file
  3. close the file

Clickherefor the data file used in the code


# Author: A Knifton
# Date: 6 Apr 2019
# Purpose:Read a text file line by line

#
# read all the text into a single data item
fHandler = open("FootballTeams.txt", "r")
for line in fHandler:
	line = line.strip("\n") # remove the newline character
	print(line)

# now close the file
fHandler.close()