Python : File Handling – Reading file line by line

python

This post is a continuation of the post Python : Basics of File Handling

The below program prompts user to enter the full path of a file to be read, and prints each line one at a time, inserting a line of asterisks between each line.

=== ===== ===

#!/usr/bin/python

def theFile():

print(“You have entered theFile function”)
fileSelection = raw_input(“Enter the file location :”)
print(“The file you have selected is : ” + str(fileSelection))
theFile = open(fileSelection)
theFileLines = theFile.readline()

while theFileLines:

print(theFileLines)
print(“*********”)
theFileLines=theFile.readline()

theFile.close()

theFile()