File Operations in Python
Reading and writing data files.
File Operations in Python is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 5. 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 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
File Operations in Python
Python makes it easy to work with files for reading and writing data. In this lesson, we’ll explore how to handle files in Python, including opening, reading, writing, and closing them.

2
Opening Files
To work with a file in Python, you first need to open it using the open() function. The open() function requires the file name and the mode (e.g., 'r' for reading, 'w' for writing).
Example:
file = open('example.txt', 'r')3
Reading Files
You can read the contents of a file using methods like read(), readline(), or readlines().
Example:
content = file.read()4
Writing Files
To write data to a file, open it in write mode ('w'). Note that this will overwrite the existing content of the file.
Example:
file = open('example.txt', 'w')
file.write('Hello, World!')5
Appending to Files
If you want to add data to an existing file without overwriting it, use append mode ('a').
Example:
file = open('example.txt', 'a')
file.write('\nNew line of text')6
Closing Files
It’s important to close a file after you’re done with it to free up system resources. Use the close() method to close the file.
Example:
file.close()7
Using the 'with' Statement
The with statement automatically handles file closing, making your code cleaner and safer.
Example:
with open('example.txt', 'r') as file:
content = file.read()8
9
Recap
You’ve learned about handling files in Python:
- Opening files: Using
open()with different modes. - Reading files: Methods like
read(). - Writing and appending: Adding content to files with write and append modes.
- Closing files: Using
close()or thewithstatement.
10
Congratulations!
You’ve completed the lesson on File Operations in Python. Continue to the next lesson to learn about error handling and debugging in Python.

Frequently asked questions
Is the “File Operations in Python” lesson free?
Yes — the full text of “File Operations in Python” is free to read here on the web, and the Learn AI with Python course includes 5 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 “File Operations in Python”?
Reading and writing data files. 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 3 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “File Operations in Python” 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.