C
PY

C to Python

10 lessons

Progress0%
1Variables & Types2Functions3Arrays → Lists4Structs → Classes & Dicts5Memory Management6String Handling7File I/O8Object-Oriented Programming9Exceptions and Context Managers10Standard Library and Tools
All Mirror Courses
C
PY
File I/O
MirrorLesson 7 of 10
Lesson 7

File I/O

Reading and writing files

Introduction

In this lesson, you'll learn about file i/o in Python. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
C
From C:

In C, you're familiar with reading and writing files.

PY
In Python:

Python has its own approach to reading and writing files, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
# Write to file — 'with' ensures file is closed
with open("output.txt", "w") as f:
    f.write("Hello, World!\n")

# Read all at once
with open("input.txt", "r") as f:
    content = f.read()

# Read line by line
with open("input.txt", "r") as f:
    for line in f:
        print(line.rstrip())

# Read all lines as list
with open("input.txt") as f:
    lines = f.readlines()

# pathlib — modern approach
from pathlib import Path
text = Path("input.txt").read_text()
Path("output.txt").write_text("Hello!")

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#include <stdio.h>
#include <string.h>

/* Write to file */
FILE *f = fopen("output.txt", "w");
if (!f) { perror("fopen"); return 1; }
fprintf(f, "Hello, World!\n");
fclose(f);

/* Read line by line */
FILE *r = fopen("input.txt", "r");
if (!r) { perror("fopen"); return 1; }
char line[256];
while (fgets(line, sizeof(line), r)) {
    /* strip newline */
    line[strcspn(line, "\n")] = '\0';
    printf("%s\n", line);
}
fclose(r);
Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C: fopen/fclose — Python: with open() (auto-closes)

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C fgets needs buffer size; Python reads strings of any size

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

Python 'with' statement replaces C's explicit fclose pattern

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

pathlib provides an object-oriented file API

Step-by-Step Breakdown

1. Context Managers (with)

Python's 'with' statement automatically closes the file even if an exception occurs — replacing C's explicit fclose.

C
C
FILE *f = fopen("file.txt", "r");
// ... use f ...
fclose(f);
PY
Python
with open("file.txt", "r") as f:
    content = f.read()
# automatically closed here

2. Reading Modes

Python's open() modes mirror C's fopen modes: 'r' read, 'w' write, 'a' append, 'rb'/'wb' for binary.

Rule of Thumb
Use 'rb'/'wb' for binary files; 'r'/'w' for text files.

3. pathlib

For simple file operations, pathlib.Path provides one-liners that replace multiple lines of C file handling.

PY
Python
from pathlib import Path
data = Path("config.json").read_text()
Path("output.txt").write_text("result")

Common Mistakes

When coming from C, developers often make these mistakes:

  • C: fopen/fclose — Python: with open() (auto-closes)
  • C fgets needs buffer size; Python reads strings of any size
  • Python 'with' statement replaces C's explicit fclose pattern
Common Pitfall
Don't assume Python works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • fopen/fclose → with open() (auto-close)
  • fgets with buffer → f.readline() or iterate file object
  • pathlib for simple read/write one-liners
  • No buffer overflow concerns in Python
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Python to practice these concepts.
PreviousNext