DEV Community

Nitinn S Kulkarni
Nitinn S Kulkarni

Posted on

File Handling in Python – Reading, Writing, and Managing Files

File handling is an essential part of programming, allowing us to read, write, and manipulate files. Python provides built-in functions to work with different file formats, including text files, CSV, JSON, and binary files.

In this post, we’ll cover:

✅ Opening and closing files in Python
✅ Reading and writing text files
✅ Handling CSV and JSON files
✅ Best practices for file handling

Let’s get started! 🚀

1️⃣ Opening and Closing Files in Python

Python provides the open() function to work with files.

🔹 Basic Syntax:

file = open("example.txt", "r")  # Open file in read mode
content = file.read()  # Read file content
file.close()  # Close the file after use
Enter fullscreen mode Exit fullscreen mode

✅ Using with Statement (Best Practice)

The with statement automatically closes the file after use, avoiding potential memory leaks.

with open("example.txt", "r") as file:
    content = file.read()  
    print(content)  # File is automatically closed after this block
Enter fullscreen mode Exit fullscreen mode

🔥 Why Use with?

✅ No need to call close() manually.
✅ Prevents issues if an exception occurs.
✅ Improves readability and safety.

2️⃣ Reading Files in Python

Python provides multiple ways to read file content:

✅ 1. Reading the Entire File (read())


with open("example.txt", "r") as file:
    content = file.read()  # Reads entire file
    print(content)
Enter fullscreen mode Exit fullscreen mode

✅ 2. Reading Line by Line (readline())


with open("example.txt", "r") as file:
    first_line = file.readline()  # Reads first line
    print(first_line)
Enter fullscreen mode Exit fullscreen mode

✅ 3. Reading All Lines (readlines())


with open("example.txt", "r") as file:
    lines = file.readlines()  # Returns a list of lines
    for line in lines:
        print(line.strip())  # Remove newline characters
Enter fullscreen mode Exit fullscreen mode

3️⃣ Writing Files in Python

Python allows writing to files using different modes:

Mode Description
"w" Write mode (overwrites file if exists)
"a" Append mode (adds to file without overwriting)
"x" Create mode (fails if file already exists)

✅ 1. Writing to a File (write())


with open("output.txt", "w") as file:
    file.write("Hello, Python!\n")
    file.write("This is a new line.\n")
Enter fullscreen mode Exit fullscreen mode

📌 Note: "w" mode overwrites the file if it exists.

✅ 2. Appending to a File (a mode)


with open("output.txt", "a") as file:
    file.write("Appending this line to the file.\n")
Enter fullscreen mode Exit fullscreen mode

4️⃣ Working with CSV Files in Python

CSV (Comma-Separated Values) files store tabular data. Python’s csv module makes it easy to read and write CSV files.

✅ 1. Reading a CSV File


import csv

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)  # Each row is a list
Enter fullscreen mode Exit fullscreen mode

✅ 2. Writing to a CSV File


import csv

with open("output.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Name", "Age", "City"])  # Header row
    writer.writerow(["Alice", 30, "New York"])
    writer.writerow(["Bob", 25, "London"])
Enter fullscreen mode Exit fullscreen mode

5️⃣ Working with JSON Files in Python

JSON (JavaScript Object Notation) is widely used for storing structured data. Python’s json module allows easy parsing and writing of JSON files.

✅ 1. Reading a JSON File


import json

with open("data.json", "r") as file:
    data = json.load(file)  # Convert JSON to dictionary
    print(data)
Enter fullscreen mode Exit fullscreen mode

✅ 2. Writing to a JSON File


import json

data = {
    "name": "Alice",
    "age": 30,
    "city": "New York"
}

with open("output.json", "w") as file:
    json.dump(data, file, indent=4)  # Write JSON with indentation
Enter fullscreen mode Exit fullscreen mode

6️⃣ Handling Binary Files (Images, Audio, etc.)

Python can also work with binary files like images and audio.

✅ Reading a Binary File


with open("image.jpg", "rb") as file:  # Open in binary mode
    data = file.read()
    print("Binary data length:", len(data))
Enter fullscreen mode Exit fullscreen mode

✅ Writing a Binary File


with open("copy.jpg", "wb") as file:
    file.write(data)  # Writing binary data
Enter fullscreen mode Exit fullscreen mode

7️⃣ Handling Exceptions While Working with Files

Handling errors ensures smooth execution.


try:
    with open("nonexistent.txt", "r") as file:
        content = file.read()
except FileNotFoundError:
    print("Error: File not found!")
except Exception as e:
    print(f"An error occurred: {e}")
Enter fullscreen mode Exit fullscreen mode

✅ Why Use Exception Handling?

Prevents crashes if the file doesn’t exist.
Helps provide meaningful error messages.
Enter fullscreen mode Exit fullscreen mode




8️⃣ Best Practices for File Handling in Python

✅ Use the with statement to ensure files are closed properly.
✅ Always handle exceptions when working with files.
✅ Use absolute paths for reliability.
✅ Avoid reading large files into memory—use streaming techniques instead.
✅ Use appropriate file modes ("r", "w", "a", "rb", "wb") based on requirements.

🔹 Conclusion

✅ Text Files – Read and write using "r" and "w".
✅ CSV Files – Use the csv module for structured data.
✅ JSON Files – Convert Python dictionaries to JSON.
✅ Binary Files – Handle images and non-text files.
✅ Exception Handling – Avoid crashes by managing errors properly.

Mastering file handling is essential for working with real-world applications. 🚀

What’s Next?

In the next post, we’ll explore Python’s Concurrency Model, covering threads, multiprocessing, and async programming. Stay tuned! 🔥

💬 What Do You Think?

How do you use file handling in your projects? What’s your preferred way to manage files? Let’s discuss in the comments! 💡

Top comments (0)