Tutorial

How to Read and Write Files in Python: A Complete Beginner’s Guide

Learn how to read, write, and manage text files in Python using open(), read(), write(), and with statements, with clear step-by-step examples.

Drake Nguyen

Founder · System Architect

3 min read

Introduction

Python is a powerful language for processing data. Many real-world programs involve reading, writing, or transforming data stored in files. For this reason, understanding file handling is a fundamental Python skill.

Whether you are managing user permissions, processing logs, or storing application output, Python provides simple and flexible tools for working with files.

In this guide, you will learn how to open, read, write, and close text files in Python 3. By the end, you will be able to confidently work with any plain text file.

Prerequisites

To follow along, you should have Python 3 installed and a basic programming environment set up on your system.

Supported File Types in Python

Python can handle many file formats, including:

File Type Description
Plain Text Stores unstructured character-based data
CSV Tabular data separated by commas or other delimiters
HTML Structured markup used for web pages
JSON Lightweight format for structured data exchange

This tutorial focuses on working with plain text files.

Step 1 — Creating a Text File

Create a new file named days.txt and add the following content:


Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
  

Save the file and note its location on your system.

Step 2 — Opening a File

In Python, files are opened using the built-in open() function. This function returns a file handle that allows you to interact with the file.


path = "/home/sammy/days.txt"
days_file = open(path, "r")
  

Common file modes include:

  • r — Read
  • w — Write (overwrite)
  • a — Append
  • r+ — Read and write

Step 3 — Reading a File

Using read()

Reads the entire file as a single string.


days_file.read()
  

Using readline()

Reads one line at a time.


days_file.readline()
  

Using readlines()

Returns all lines as a list of strings.


days_file.readlines()
  

Once a file is read, its internal pointer moves forward. To read again, the file must be reopened or repositioned.

Step 4 — Writing to a File

You can create a new file or overwrite an existing one using write mode.


title = "Days of the Week\n"
new_path = "/home/sammy/new_days.txt"

new_days = open(new_path, "w")
new_days.write(title)
new_days.write(days)
  

Be careful when using w mode, as it overwrites existing files.

Step 5 — Closing Files

Always close files when finished to release system resources.


days_file.close()
new_days.close()
  

Step 6 — Using with Statements (Recommended)

The Pythonic way to work with files is using with statements, which automatically handle closing files.


with open(path, "r") as days_file, open(new_path, "w") as new_days:
    days = days_file.read()
    new_days.write(title)
    new_days.write(days)
  

This approach reduces errors and keeps file-related logic clean and readable.

Step 7 — Final Example


path = "/home/sammy/days.txt"
new_path = "/home/sammy/new_days.txt"
title = "Days of the Week\n"

with open(path, "r") as days_file, open(new_path, "w") as new_days:
    days = days_file.read()
    new_days.write(title)
    new_days.write(days)

print(title)
print(days)
  

Conclusion

In this tutorial, you learned how to open, read, write, and close text files in Python. File handling is a foundational skill that enables you to build data-driven applications and automation scripts.

As you continue learning Python, you can explore advanced file formats, error handling, and performance optimization techniques.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.