Home of real teaching & learning
  • Full support for teachers
  • Focus on critical thinking
  • Engaging classroom activities
  • Integrated student eBook
  • Assessed tasks / qBank
  • Practice exam questions

The InThinking Guarantee: Our sites are written by expert practitioners and not by AI

See our AI policy

Disclaimer: InThinking subject sites are neither endorsed by nor connected with the International Baccalaureate Organisation.

Don't miss out, find out!

B 2.5 Visualising File Operations - Worksheet - Python

Name: ___________________________________________________________________ Date: _____________________  

Part 1: The Mental Model

In Python, when you open a file, imagine an invisible "cursor" (like the blinking line in a Word document) that points to where the computer will read or write next.

The Golden Rule: You cannot read what is behind the cursor without moving it back.

Part 2: The "Ghost File" Scenario

Analyse the following code snippet. It runs without errors, but the variable content ends up empty.

# Code Snippet A
file = open("my_data.txt", "w+")      # Open for reading AND writing
file.write("Hello World")             # Write data
content = file.read()                 # Try to read data
print(f"Data found: {content}")       # Print result
file.close()

Task 1: Draw the Cursor. Below is a representation of the file buffer after line 2 (file.write("Hello World")) runs. Draw an arrow ^ showing where the "file cursor" is currently sitting.

| H | e | l | l | o |   | W | o | r | l | d |

Task 2: The Logic Error. Explain why file.read() returns an empty string in the code above.

Part 3: Fixing the Bug

There are two ways to fix the "Ghost File" bug. Write the missing line of code for each solution.

Solution A: The "Seek" Method: Move the cursor back to the start manually before reading.

file.write("Hello World")
# [Write the missing line below]
________________________________
content = file.read()

Solution B: The "Close & Reopen" Method Save the data properly, close the file, and open it again in a different mode.

file = open("my_data.txt", "w")
file.write("Hello World")
# [Write the missing lines below]
________________________________
________________________________
content = file.read()

Part 4: Challenge - The "Append" Trap A student wants to add a new score to a list of high scores. They run this code:

file = open("scores.txt", "w")  # Danger!
file.write("Player1: 500")
file.close()

What happens to the old scores that were already in the file? 

[ ] They are moved to the bottom. 

[ ] They are kept safely at the top. 

[ ] They are completely deleted.

How should line 1 be changed to fix this?

 __________________________________________________________________________

All materials on this website are for the exclusive use of teachers and students at subscribing schools for the period of their subscription. Any unauthorised copying or posting of materials on other websites is an infringement of our copyright and could result in your account being blocked and legal action being taken against you.

Help