Skip to main content

File Handling 📁

Important Note 🚨

File handling operations may not work seamlessly in all environments. If you’re running this in a browser-based Python environment, like an online editor, file operations might be restricted or unavailable. Try running it locally for the best experience!

File handling is one of those grown-up parts of programming. You know, like keeping receipts or doing your taxes. But don’t worry—it’s much simpler and a lot more fun. With Python, you can write to files, read from files, and even poke around to see what’s inside. Let’s dive in! 🕵️‍♂️


Writing to Files 🖊️

Writing to files is like sending a letter to the future. You put your thoughts (or data) in a file, and it stays there until you (or someone else) needs it.

Pseudo-Code Example:

DECLARE TextLine : STRING  
DECLARE MyFile : STRING
MyFile ← "MyText.txt"
OPEN MyFile FOR WRITE
OUTPUT "Please enter a line of text"
INPUT TextLine
WRITEFILE, TextLine
CLOSEFILE(MyFile)

Python Translation:

# Writing to a file  
TextLine = input("Please enter a line of text: ")
with open("MyText.txt", "w") as MyFile:
MyFile.write(TextLine + "\n") # Adding a newline for neatness
print("Text written to MyText.txt!")

What happens in the file?

If you entered "Hello, world!", the file MyText.txt will now contain:

Hello, world!  

Fun Example: Writing a Diary Entry 📓

Scenario: You’re writing your deepest, darkest secrets in a file.

Pseudo-Code:

DECLARE DiaryEntry : STRING  
DiaryEntry ← "Today was a great day. I ate pizza!"
OPEN "Diary.txt" FOR WRITE
WRITEFILE, DiaryEntry
CLOSEFILE("Diary.txt")

Python Translation:

DiaryEntry = "Today was a great day. I ate pizza!"  
with open("Diary.txt", "w") as DiaryFile:
DiaryFile.write(DiaryEntry)
print("Your diary has been updated!")

Output:
Your diary has been updated! Let’s hope no one opens it without permission! 🔐


Reading from Files 📖

Reading files is like opening a treasure chest—you never know what you might find. Let’s see how to do it.

Pseudo-Code Example:

DECLARE MyFile : STRING  
DECLARE TextLine : STRING
MyFile ← "MyText.txt"
OPEN MyFile FOR READ
READFILE, TextLine
OUTPUT "The file contains:", TextLine
CLOSEFILE(MyFile)

Python Translation:

# Reading from a file  
with open("MyText.txt", "r") as MyFile:
TextLine = MyFile.readline().strip() # Remove any extra whitespace
print("The file contains:", TextLine)

What happens here?

If MyText.txt contains "Hello, world!", the output will be:

The file contains: Hello, world!  

Fun Example: Reading a Magic Spell Book 🧙‍♀️

Scenario: You found a file with spells. Let’s see what’s inside!

Pseudo-Code:

DECLARE Spell : STRING  
OPEN "SpellBook.txt" FOR READ
READFILE, Spell
OUTPUT "You read the spell: ", Spell
CLOSEFILE("SpellBook.txt")

Python Translation:

with open("SpellBook.txt", "r") as SpellBook:  
Spell = SpellBook.readline().strip()
print("You read the spell:", Spell)

Output:
You read the spell: Wingardium Leviosa (Or whatever magical words you wrote!) ✨


Combining Writing and Reading

Why stop at one? Let’s write something, then immediately read it back.

Scenario: Writing a list of favourite foods and reading them aloud.

Pseudo-Code:

DECLARE Food : STRING  
Food ← "Pizza, Pasta, Ice Cream"
OPEN "Foods.txt" FOR WRITE
WRITEFILE, Food
CLOSEFILE("Foods.txt")
OPEN "Foods.txt" FOR READ
READFILE, Food
OUTPUT "Your favourite foods are: ", Food
CLOSEFILE("Foods.txt")

Python Translation:

# Writing  
Food = "Pizza, Pasta, Ice Cream"
with open("Foods.txt", "w") as FoodsFile:
FoodsFile.write(Food)

# Reading
with open("Foods.txt", "r") as FoodsFile:
Food = FoodsFile.readline().strip()
print("Your favourite foods are:", Food)

Output:
Your favourite foods are: Pizza, Pasta, Ice Cream


Did you know?

Using with open() in Python is a good habit. It automatically closes the file when you’re done, saving you the hassle of remembering to close it. Think of it as the butler of file handling. 🕴️

File handling might sound boring, but trust us, it’s the first step towards writing your future bestseller or keeping your evil plans safe. Go ahead, give it a whirl! 🚀