CODE PATH
PRO ACCOUNT

Master Code
On The Go

Learn. Practice. Build.

Quest 2 β€’ Lesson 1

πŸ“‚ File I/O (Read & Write)

Learn how to read from and write to files – essential for real‑world Python applications.

File I/O (Input/Output) allows your Python programs to work with files on disk. You can read data from files, write data to files, and append to existing files.

"The with statement automatically closes the file for you – preventing memory leaks and corruption."

πŸš€ Live Demo

Simulate writing to and reading from a file.

File content will appear here.

🧠 Writing to a File

with open("data.txt", "w") as file:
    file.write("Hello, world!")

πŸ“– Reading from a File

with open("data.txt", "r") as file:
    content = file.read()
    print(content)

πŸ“‹ File Modes

"r" – read (default)
"w" – write (overwrites)
"a" – append (adds to end)
"r+" – read and write
"rb" – read binary
"wb" – write binary
file_io.py
# Write to a file
with open("notes.txt", "w") as f:
    f.write("Python is awesome!\n")
    f.write("I'm learning file I/O.")

# Read from a file
with open("notes.txt", "r") as f:
    content = f.read()
    print(content)

✨ Challenge: Write a Log File

Write a program that:

➑️ Next Lesson

Lesson 2.2: Modules & Packages – organise and reuse code.

Continue to Lesson 2.2 β†’

(Coming soon – check back or buy Pro Pack for early access)

❀️ Support Free Education

This course is 100% free. If it helps you, consider buying me a coffee.

β˜• Buy Me a Coffee
← Back to Python Course Hub