LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Python

Python - File Operations & Data Formats

Use the with statement to safely open and close files automatically, and use Python's built-in csv and json modules to parse structured data reliably.

File Operations and Data Formats in Python – Safe Resource Management with Context Managers, CSVs, and JSON

Quick Answer: Use the with statement to safely open and close files automatically, and use Python's built-in csv and json modules to parse structured data reliably.

Imagine Priya manages daily grocery inventory logs and customer orders at her shop in Bengaluru.

She records daily sales in text notes, exports structured revenue reports to CSV files, and saves system configurations in JSON.

If a script crashes while writing data without closing the file, disk records get corrupted. How do we write robust code that handles files safely?

1. Why Do We Need Context Managers and Structured Data Formats?

The problem without the with statement

We could open files manually using open() and close them using close().

Example 1: Manual file handling without a context manager

This code works, but if an error occurs before close(), the file remains locked in memory.

f = open("sales.txt", "r")
content = f.read()
print(content)
f.close()

Expected output

Rice: 55.0
Sugar: 42.0

This code runs successfully under normal conditions, but it has major safety flaws.

  1. If an exception occurs before f.close(), the file handle leaks.
  2. Manual cleanup creates memory leaks in long-running backend applications.
  3. Raw text files require custom string splitting for columns and structured data.

We need an automated safety wrapper and standard formats for tabular and nested data.

The solution: context managers and parsers

Context manager = an object that manages resource setup and cleanup automatically using the with statement.

CSV = Comma-Separated Values format used for storing tabular spreadsheet data.

JSON = JavaScript Object Notation format used for storing structured key-value pairs.

Resource Lifecycle in Priya's Shop Automation
How context managers guarantee safe opening and closing
Open Resource Acquire file lock safely
Enter Block
Read / Write Process CSV or JSON
Auto Close Guaranteed cleanup
Safe State No memory leaks
Setup Active Step Result State

Figure 1: The with statement automatically executes cleanup code when exiting the block.

2. Safe Resource Handling with the with Statement

Let us write a complete runnable Python program that reads and writes text files safely using a context manager.

Example 2: Writing and reading text files with with

This code writes daily sales notes and reads them back with guaranteed file closure.

if __name__ == "__main__":
    filename = "daily_sales.txt"
    
    # Writing data safely
    with open(filename, "w") as f:
        f.write("Rice: 5500\nSugar: 4200\n")
        
    # Reading data safely
    with open(filename, "r") as f:
        data = f.read()
        print("--- Sales Log ---")
        print(data, end="")

Expected output

--- Sales Log ---
Rice: 5500
Sugar: 4200

Notice that we do not need to call f.close() explicitly.

Python handles file closure automatically as soon as the execution leaves the indented block.

3. Working with Tabular Data Using CSV Files

PlainText logs work for simple notes, but spreadsheets require rows and columns.

Python provides the built-in csv module to read and write tabular records without manual string splitting.

Example 3: Parsing and writing CSV inventory data

This code reads stock prices from a CSV file using the csv.reader utility.

import csv
import os

if __name__ == "__main__":
    csv_file = "inventory.csv"
    
    # Create sample CSV file
    with open(csv_file, "w", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Item", "Price", "Stock"])
        writer.writerow(["Rice", "55.0", "120"])
        writer.writerow(["Sugar", "42.0", "85"])
        
    # Read and parse CSV file
    with open(csv_file, "r") as f:
        reader = csv.reader(f)
        for row in reader:
            print(row)
            
    os.remove(csv_file)

Expected output

['Item', 'Price', 'Stock']
['Rice', '55.0', '120']
['Sugar', '42.0', '85']

Each row is parsed automatically into a Python list of strings.

This avoids comma-delimiter bugs when item names contain special characters or commas.

4. Parsing and Serializing JSON Data

JSON is the standard format for API payloads and configuration files.

Python's built-in json module converts dictionaries into JSON strings (serialization) and JSON back into dictionaries (parsing).

Example 4: Reading and writing JSON configurations

This code saves shop settings into a JSON file and loads them back.

import json
import os

if __name__ == "__main__":
    json_file = "shop_config.json"
    config = {
        "store_name": "Priya Provision Store",
        "city": "Bengaluru",
        "open_status": True
    }
    
    # Serialize dictionary to JSON file
    with open(json_file, "w") as f:
        json.dump(config, f, indent=4)
        
    # Parse JSON file back to dictionary
    with open(json_file, "r") as f:
        loaded_data = json.load(f)
        print(loaded_data["store_name"])
        print(loaded_data["city"])
        
    os.remove(json_file)

Expected output

Priya Provision Store
Bengaluru

Using json.dump() writes formatted dictionaries to disk.

Using json.load() reads JSON files directly into native Python data types.

Comparing manual file management with context managers and structured parsers
Manual File Handling Context Managers & Parsers
Requires explicit close() calls Automatic resource cleanup via with
Prone to resource leaks on exceptions Guaranteed safety even when errors occur
Manual string splitting for CSV/JSON Built-in csv and json modules

The important terms to remember

  • Context Manager = an object managing resource allocation and cleanup.
  • with statement = Python syntax for invoking context managers.
  • Serialization = converting Python objects into storage formats like JSON.
  • Parsing = reading raw text or structured formats into Python objects.

Design tip: Always use context managers when interacting with files, database connections, or network sockets.

5. Common Mistakes to Avoid

1. Forgetting file encoding on cross-platform systems

Opening files without specifying UTF-8 encoding can cause Unicode decode errors on Windows systems.

Better approach: Always pass encoding="utf-8" when opening text, CSV, or JSON files.

6. Interview Questions & Answers

Question 1: Why should you use the with statement when working with files in Python?

What the interviewer expects: A clear explanation of automatic resource management and exception safety.

Strong answer: "The with statement ensures that resources like file handles are cleaned up automatically as soon as the execution block exits. Even if an exception occurs inside the block, Python guarantees the file is closed properly, preventing memory leaks and file locks."

What interviewers expect at different levels

  • Junior developer: Knows how to use with open() instead of manual close().
  • Senior developer: Understands Python's Context Manager protocol (`__enter__` and `__exit__`).
  • Technical architect: Designs robust custom context managers for transaction and connection pooling.

7. Practice Exercises

Beginner: Daily expense logger

Write a script that appends three expense items to a text file using a context manager.

Expected result: File contains all three expenses written on separate lines.

Intermediate: CSV revenue calculator

Read a CSV file containing item prices and quantities, then calculate and print total sales revenue.

Hint: Convert string columns to floating-point numbers before performing arithmetic.

8. Frequently Asked Questions (FAQ)

Can you open multiple files in a single with statement?

Yes. You can chain multiple context managers using commas or parentheses in Python.

What is the difference between json.load() and json.loads()?

json.load() reads JSON directly from a file object, while json.loads() parses JSON from a string.

9. Next Steps

Now that you have mastered file operations and data formats, you are ready to connect Python to relational databases like MS SQL Server in the next article.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.