Computer Science · Ch 2 — File Handling in Python
Traversing a file and displaying data
Traversing a file and displaying data
To read data back from a text file, you must open it in read mode ("r"). Once opened, reading begins from the very first byte of the file — the file pointer is at position 0. The textbook builds this idea step by step: first by creating a file with data (Program 2-3, covered in the previous section), then reading it back line by line, and finally combining both reading and writing in a single program.
Reading the file line by line (Program 2-4)
fileobject=open("practice.txt","r")
str = fileobject.readline()
while str:
print(str)
str=fileobject.readline()
fileobject.close()
Program 2-4 opens practice.txt in read mode ("r"). It uses a while loop with the readline() method to fetch one line at a time. The loop condition checks the string returned by readline() — as long as it is not an empty string, the loop continues. When the end of the file is reached, readline() returns an empty string, and the loop stops. Inside the loop, each line is printed using print(). After the loop finishes, the file is closed with close().
Output of Program 2-4:
>>>
I am interested to learn about Computer SciencePython is easy to learn
The output shows the two lines printed without a space between them. This happens because the original file was written without newline characters. The readline() method reads the entire line including the newline if present, but here there is none, so the second line sticks to the end of the first.
Reading and writing in one program (Program 2-5)
fileobject=open("report.txt", "w+")
print ("WRITING DATA IN THE FILE")
print() # to display a blank line
while True:
line= input("Enter a sentence ")
fileobject.write(line)
fileobject.write('\n')
choice=input("Do you wish to enter more data? (y/n): ")
if choice in ('n','N'): break
print("The byte position of file object is ",fileobject.tell())
fileobject.seek(0) #places file object at beginning of file
print()
print("READING DATA FROM THE FILE")
str=fileobject.read()
print(str)
fileobject.close()
The textbook then combines both operations into a single program using one file object. The file report.txt is opened in "w+" mode, which allows both writing and reading. The program first writes data to the file, asking the user to enter sentences one by one; each sentence is written using fileobject.write(line), followed by an explicit newline character '\n'. The loop continues until the user enters 'n' or 'N'.
After writing, the program prints the current byte position using fileobject.tell() — in the example output, this is 67.
After writing, the file pointer is at the end of the file. To read the data you just wrote, you must move the file pointer back to the beginning. This is done using fileobject.seek(0). Without this, any read operation would start from the end and return nothing.
After seek(0), the program reads the entire file content at once using fileobject.read() and prints it.
Output of Program 2-5:
>>>
RESTART: Path_to_file\Program2-5.py
WRITING DATA IN THE FILE
Enter a sentence I am a student of class XII
Do you wish to enter more data? (y/n): y
Enter a sentence my school contact number is 4390xxx8
Do you wish to enter more data? (y/n): n
The byte position of file object is 67
READING DATA FROM THE FILE …