JV
PY

Java to Python

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Collections4Exception Handling5File I/O6Functional Programming7Duck Typing and Protocols8Python Ecosystem9Type Hints and Static Analysis10Context Managers and Resources
All Mirror Courses
JV
PY
File I/O
MirrorLesson 5 of 10
Lesson 5

File I/O

Reading and writing files

Introduction

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

Mirror Card
JV
From Java:

In Java, 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
from pathlib import Path

# Read all text
content = Path("input.txt").read_text()

# Write
Path("output.txt").write_text("Hello!")

# Line by line — with auto-close
with open("input.txt") as f:
    for line in f:
        print(line.rstrip())

# List directory
for p in Path(".").iterdir():
    print(p)

# Check exists / glob
if Path("config.txt").exists():
    configs = list(Path(".").glob("*.json"))

Comparing to Java

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

JV
Java (What you know)
import java.nio.file.*;
import java.io.*;

// Read all text (Java 11+)
String content = Files.readString(Path.of("input.txt"));

// Write
Files.writeString(Path.of("output.txt"), "Hello!");

// Line by line
try (BufferedReader br = new BufferedReader(
        new FileReader("input.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}

// List directory
Files.list(Path.of("."))
    .forEach(System.out::println);
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python pathlib.Path = Java's java.nio.file.Path

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python 'with open()' = Java try-with-resources

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python Path.read_text() = Java Files.readString()

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python iteration over file object = Java BufferedReader loop

Step-by-Step Breakdown

1. One-liner Read/Write

Python's pathlib provides clean one-liners for common operations, similar to Java's Files.readString/writeString (Java 11+).

JV
Java
Files.readString(Path.of("file.txt"))
PY
Python
Path("file.txt").read_text()

2. with vs try-with-resources

Python's 'with' statement auto-closes files like Java's try-with-resources, but with cleaner syntax.

JV
Java
try (BufferedReader br = new BufferedReader(new FileReader("f.txt"))) { ... }
PY
Python
with open("f.txt") as f: ...

3. pathlib vs java.nio

Python's pathlib.Path object provides file operations as methods — similar to Java's Path + Files API.

JV
Java
Files.exists(path); Files.list(path)
PY
Python
path.exists(); list(path.iterdir())

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Python pathlib.Path = Java's java.nio.file.Path
  • Python 'with open()' = Java try-with-resources
  • Python Path.read_text() = Java Files.readString()
Common Pitfall
Don't assume Python works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • pathlib.Path = java.nio.file.Path + Files
  • with open() = try-with-resources
  • read_text()/write_text() = readString()/writeString()
  • Iterate file object directly for line-by-line
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in Python to practice these concepts.
PreviousNext