DEV Community

Sameer Qaisar
Sameer Qaisar

Posted on

Python Files: Reading and Writing for Beginners

πŸ“Œ Quick Info

  • Topic: Reading and writing files in Python
  • Target Audience: Beginners who know the basics but haven't worked with files yet
  • Goal: Understand how to save data to a file and read it back

1. Introduction

"For weeks, every Python program I wrote forgot everything the moment it closed. Then I learned how to work with files β€” and suddenly my programs could remember."

2. The Problem (Data Disappears)

name = input("What's your name? ")
print(f"Hello, {name}!")
Enter fullscreen mode Exit fullscreen mode

Run it once, it works. Run it again tomorrow, it asks again.

The data disappears the moment the program ends. Nothing is saved.

3. The Solution (Writing to a File)

with open("names.txt", "w") as file:
    file.write("Ali\n")
    file.write("Sara\n")
    file.write("Ahmed\n")

print("Saved!")
Enter fullscreen mode Exit fullscreen mode

What happens: A file called names.txt is created with three names.

4. Reading From a File

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

print(content)
Enter fullscreen mode Exit fullscreen mode

Output:

Ali
Sara
Ahmed
Enter fullscreen mode Exit fullscreen mode

The data is still there β€” even after the program closed.

5. How It Works (Line by Line)

Line 1: with open("names.txt", "w") as file: β€” Opens the file in write mode ("w").

Line 2-4: file.write(...) β€” Writes each name, plus \n to start a new line.

The with keyword: Automatically closes the file when you're done. You don't have to remember to close it.

6. The Three File Modes

Mode What It Does
β€œr” Read only. Throws an error if the file doesn’t exist.
β€œw” Write. Erases the file if it already exists.
β€œa” Append. Adds to the end without erasing.

Warning: "w" overwrites everything. Use "a" if you want to keep existing content.

7. Real Example (My Practise)

with open("scores.txt", "a") as file:
    file.write("92\n")
    file.write("85\n")

with open("scores.txt", "r") as file:
    for line in file:
        print(line.strip())
Enter fullscreen mode Exit fullscreen mode

Output:

92
85
Enter fullscreen mode Exit fullscreen mode

line.strip() removes the extra newline character.

8. What I Learned

-) Files let data survive after the program ends
-) "r" reads, "w" writes (and erases), "a" appends
-) with open(...) handles closing for you β€” always use it
-) .strip() cleans up extra whitespace
-) Always check that a file exists before reading it.

9. Conclusion

"Files felt scary at first β€” I was worried I'd overwrite something important. But once I understood the three modes, files became one of the most useful tools in my Python toolbox. Programs that remember are programs people actually use."

Top comments (0)