Find the output of the following Python code based on a text file data.txt…
2026
Find the output of the following Python code based on a text file data.txt which contains exactly the string:
Learning_Python_Is_Fun
def manipulate_file():
with open("data.txt", "w+") as f:
f.write("Learning_Python_Is_Fun")
f.seek(9)
part1 = f.read(6)
f.seek(0, 2) # Move to the end
pos = f.tell()
f.seek(0)
line = f.readline().split('_')
print(part1)
print(pos)
print(line[1])
print(line[-1])
manipulate_file()
Attempted by 21 students.
Show answer & explanation
File is opened in
"w+"mode ⇒ it allows read/write and overwrites existing content.The string
"Learning_Python_Is_Fun"is written to the file.Total length of the string is 22 characters (including
_).f.seek(9)moves the pointer to index 9 (character'P').f.read(6)reads"Python"⇒ part1 = Pythonf.seek(0, 2)moves pointer to end ⇒tell()returns 22f.seek(0)andreadline().split('_')⇒['Learning','Python','Is','Fun']Final Outputs:
Python
22
Python
Fun