TalentJR

Lesson 17

Working with Files

Read and write files

1Reading Files✨

💻 code
file = open("data.txt", "r")
content = file.read()  # Read all
file.close()

2Reading Line by Line🎯

💻 code
file = open("data.txt", "r")
for line in file:
    print(line.strip())
file.close()

3Writing Files💡

💻 code
file = open("output.txt", "w")
file.write("Hello, World!")
file.close()

4Appending to Files🚀

💻 code
file = open("log.txt", "a")
file.write("New entry\n")
file.close()
💻 code
With Statement (Best Practice)
with open("data.txt", "r") as file:
    content = file.read()
# File auto closes

5File Examples🔍

💻 code
# Save scores
with open("scores.txt", "w") as file:
    for name, score in scores.items():
        file.write(f"{name}: {score}\n")
💻 code
# Read scores
with open("scores.txt", "r") as file:
    for line in file:
        name, score = line.strip().split(":")
        print(f"{name} scored {score}")

🎯 Practice Challenges

Master Working with Files by completing all 3 difficulty levels

🟢 EASY

Working with Files - Easy Challenge

Start with the basics. Perfect for beginners!

🟡 MEDIUM

Working with Files - Medium Challenge

Get more challenging. Build your confidence!

🔴 HARD

Working with Files - Hard Challenge

Master this concept. Prove your skills!

🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →

Ready to Master Working with Files? 🚀

Complete the challenges, take the quiz and keep your learning streak going!