0Pricing
Learn AI with Python · Lesson

Reading and Writing Files

Understand how to handle files to store and retrieve data.

Reading and Writing Files is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Reading and Writing Files

File handling is an essential skill for any Python programmer. It allows you to store, retrieve, and manipulate data in files on your computer.

In this lesson, you’ll learn how to open, read, write, and close files using Python's built-in functions.

Reading and Writing Files — illustration 1

2

Opening Files

To work with a file in Python, you first need to open it using the open() function. The function takes the file name and mode as arguments:

  • 'r': Read mode (default).
  • 'w': Write mode.
  • 'a': Append mode.
  • 'b': Binary mode.
# Opening a file in read mode
file = open("example.txt", "r")
# Always remember to close the file after use
file.close()

3

4

Reading Files

You can read the contents of a file using methods like read(), readline(), and readlines():

# Reading the entire content of a file
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()

5

Writing to Files

To write to a file, open it in write ('w') or append ('a') mode. Writing in 'w' mode overwrites the file, while 'a' mode appends to it:

# Writing to a file
file = open("example.txt", "w")
file.write("Hello, World!\n")
file.close()

6

Using the with Statement

The with statement simplifies file handling by automatically closing the file:

# Using with statement to open and read a file
with open("example.txt", "r") as file:
    content = file.read()
    print(content)  # File is automatically closed after this block

7

Appending to Files

Appending allows you to add new content to the end of an existing file without overwriting it:

# Appending to a file
with open("example.txt", "a") as file:
    file.write("This is additional content.\n")

8

Reading Files Line by Line

You can use a loop to read a file line by line, which is useful for large files:

# Reading a file line by line
with open("example.txt", "r") as file:
    for line in file:
        print(line.strip())  # Removes the trailing newline

9

10

Common Mistakes with File Handling

Here are some mistakes to avoid:

  • Forgetting to close a file after opening it (use with statement).
  • Using write mode ('w') unintentionally, which overwrites the file.
  • Not handling file-related exceptions.

11

What Did We Learn?

In this lesson, you learned:

  • How to open, read, write, and append to files in Python.
  • How to use the with statement for safer file handling.
  • Common mistakes to avoid when working with files.

Great job! Let’s move to the next topic.

Reading and Writing Files — illustration 11

Frequently asked questions

Is the “Reading and Writing Files” lesson free?

Yes — the full text of “Reading and Writing Files” is free to read here on the web, and the Learn AI with Python course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Reading and Writing Files”?

Understand how to handle files to store and retrieve data. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Reading and Writing Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading and Writing Files
  2. Working with CSV Files
  3. Handling Exceptions in File I/O
← Back to Learn AI with Python