Writing to Files
Save your data into a file.
Writing to Files is a free Python For Kids lesson on CoddyKit — lesson 2 of 4. 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 Python For Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Writing to Files in Python
Welcome back to the File Handling lesson! Today, we'll learn how to write data to files in Python. Writing to files lets you save data generated by your program for future use.

2
Why Write to Files?
Writing data to files is essential for many applications, such as:
- Data Storage: Save results or data for future use.
- Logging: Keep a record of events or errors.
- Reports: Generate reports that can be shared or analyzed later.
Let's see how to write to files in Python!
3
Opening a File for Writing
To write to a file, open it using the open() function with the mode 'w' (write mode). If the file does not exist, it will be created. If it does exist, its content will be overwritten.
Syntax:
file_object = open("filename", "w")
4
Writing Text to a File
You can use the write() method to write a string to a file. Don't forget to close the file after writing!
Example:
# Opens the file in write mode
file = open("output.txt", "w")
# Writes a string to the file
file.write("Hello, World!")
# Closes the file
file.close()
5
Appending Data to a File
If you want to add data to an existing file without overwriting its contents, open the file in append mode ('a').
Example:
# Opens the file in append mode
file = open("output.txt", "a")
# Appends a string to the file
file.write("\nPython is great!")
# Closes the file
file.close()
6
Using the with Statement
To ensure that a file is properly closed after writing, use the with statement. This automatically handles file closing.
Example:
# Uses the with statement to write to a file
with open("output.txt", "w") as file:
file.write("This is a safer way to handle files.")
7
Writing Multiple Lines
You can write multiple lines to a file by using writelines(). This method takes a list of strings and writes them to the file.
Example:
# Uses the with statement to write multiple lines
lines = ["First line\n", "Second line\n", "Third line\n"]
with open("output.txt", "w") as file:
file.writelines(lines)
8
Overwriting vs. Appending
Be careful when opening a file in 'w' mode, as it overwrites all existing content. Use 'a' mode to add data without losing the existing content.
Example:
# Opens the file in write mode and overwrites content
with open("output.txt", "w") as file:
file.write("New content.\n")
# Opens the file in append mode and adds new content
with open("output.txt", "a") as file:
file.write("Appended content.\n")
9
# Writes to the file in write mode
with open("log.txt", "w") as file:
file.write("Log entry 1.")
10
Great Job!
You've learned how to write data to files in Python, including overwriting, appending, and writing multiple lines. Writing files is a fundamental skill for saving data and creating logs or reports. Keep practicing by experimenting with different file modes and methods!

Frequently asked questions
Is the “Writing to Files” lesson free?
Yes — the full text of “Writing to Files” is free to read here on the web, and the Python For Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.
What will I learn in “Writing to Files”?
Save your data into a file. You practise Python For Kids 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 Python For Kids?
No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing to 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 Python For Kids lesson?
Yes. Every Python For Kids 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
- Reading Files
- Writing to Files
- File Modes
- Exception Handling in File Operations