DEV Community

still-purrfect
still-purrfect

Posted on

File Handling in Python: Working With Real Data

So far, everything we’ve done exists only inside the program.
But what if your program could remember things even after it closes?
That’s where file handling comes in.
So far, we’ve worked with variables, functions, and modules.
But in real applications, data doesn’t just disappear when a program stops running.
It is saved in files like .txt, .csv, or databases.
Python allows us to work with these files using file handling.

🔹 Opening a File

To work with a file, we first open it using open().

file = open("file.txt", "r")
Enter fullscreen mode Exit fullscreen mode

The "r" means read mode (we are only reading the file).

🔹 Reading a File

Once the file is open, we can read its content.

file = open("file.txt", "r")

content = file.read()
print(content)

file.close()
Enter fullscreen mode Exit fullscreen mode

Always remember to close the file after using it.

🔹 Writing to a File

We can also create or write into files using "w" mode.

file = open("file.txt", "w")

file.write("Hello, this is my first file.")

file.close()
Enter fullscreen mode Exit fullscreen mode

⚠️ Note: Writing mode will overwrite existing content.

🔹 Appending to a File

If you don’t want to overwrite, use "a" mode.

file = open("file.txt", "a")

file.write("\nThis is an additional line.")

file.close()
Enter fullscreen mode Exit fullscreen mode

This adds new content without deleting old data.

💡 Why File Handling Matters

File handling allows your programs to:

  • Save user data
  • Store results
  • Keep records
  • Work like real applications Without it, everything resets once the program stops. With it, your program can actually remember things.

🌱 Challenge

Create a program that:

  1. Writes your name and age into a file
  2. Opens the file again
  3. Reads and prints the content Try also appending a new line after that.

Next, we’ll explore exception handling, where your program learns how to handle errors without crashing.

Top comments (1)

Collapse
 
ggle_in profile image
HARD IN SOFT OUT

Finally, data that survives after closing the terminal 😄

This is one of those "small but huge" leaps for beginners. The moment your program can remember things across runs – that's when it stops being a script and starts feeling like a real app.

The with pattern you showed in the previous article (working with .txt files) is definitely worth repeating here too:

with open("file.txt", "r") as file:
    content = file.read()
    print(content)
# No need to call close() – it's automatic
Enter fullscreen mode Exit fullscreen mode

One tiny tip I'd add: when reading config files or small datasets, .read() is fine. But for larger files, iterating line by line saves memory:


with open("big_file.txt", "r") as file:
    for line in file:
        process(line)  # handles one line at a time
Enter fullscreen mode Exit fullscreen mode

Looking forward to the exception handling article – that's where file operations can get messy (file not found, permission errors, etc.).

Great job keeping these fundamentals approachable.

Cheers,
Jack