Writing to a file is an important part of file handling in Python. It allows you to store data permanently in a file so it can be used later, even after the program stops running.
Python provides simple methods to write data into files using different modes such as w (write) and a (append).
In this tutorial, you will learn how to write to files in Python with clear examples.
What is Writing to a File?
Writing to a file means saving data from a Python program into a file stored on your computer.
You can:
- Create new files
- Overwrite existing files
- Add new content to files
File Open Modes for Writing
| Mode | Description |
|---|---|
| 'w' | Write mode (overwrites file) |
| 'a' | Append mode (adds data) |
| 'x' | Create new file |
1. Writing to a File (w mode)
The w mode writes data to a file and overwrites existing content.
Example:
file = open("example.txt", "w")
file.write("Hello Python!")
file.close()Explanation:
- Creates file if it does not exist
- Overwrites existing content
- Writes new text
2. Writing Multiple Lines
You can write multiple lines using \n.
Example:
file = open("example.txt", "w")
file.write("Line 1\n")
file.write("Line 2\n")
file.write("Line 3\n")
file.close()3. Writing Using a List of Strings
Example:
lines = ["Apple\n", "Banana\n", "Mango\n"]
file = open("example.txt", "w")
file.writelines(lines)
file.close()4. Appending to a File (a mode)
The a mode adds data without deleting existing content.
Example:
file = open("example.txt", "a")
file.write("This is appended text.\n")
file.close()5. Writing New File (x mode)
The x mode creates a new file. If the file already exists, it will show an error.
Example:
file = open("newfile.txt", "x")
file.write("This is a new file created.")
file.close()6. Using with Statement (Best Practice)
The with statement automatically closes the file.
Example:
with open("example.txt", "w") as file:
file.write("Hello from with statement!")Benefits:
- No need to call
close() - Cleaner and safer code
7. Append vs Write Difference
Write Mode (w)
file = open("example.txt", "w")
file.write("First Text")
file.close()✔ Overwrites old content
Append Mode (a)
file = open("example.txt", "a")
file.write("New Text")
file.close()✔ Keeps old content and adds new data
8. Writing User Input to File
Example:
name = input("Enter your name: ")
with open("user.txt", "w") as file:
file.write("User Name: " + name)9. Writing Logs to File
Example:
with open("log.txt", "a") as file:
file.write("System started successfully\n")Real-World Example: Student Record File
name = "Alice"
marks = 90
with open("student.txt", "w") as file:
file.write(f"Name: {name}\nMarks: {marks}")Real-World Example: Daily Notes App
note = "Today I learned Python file handling"
with open("notes.txt", "a") as file:
file.write(note + "\n")File Writing Methods Summary
| Method | Description |
| write() | Write single string |
| writelines() | Write multiple lines |
| w mode | Overwrite file |
| a mode | Append data |
| x mode | Create new file |
| with open() | Safe writing method |
Common Mistakes
Mistake 1: Forgetting newline
❌ Wrong:
file.write("Line1Line2")✔ Use \n for new lines
Mistake 2: Using wrong mode
open("file.txt", "r")✔ Cannot write in read mode
Mistake 3: Overwriting data accidentally
open("file.txt", "w")✔ Deletes old content
Safe Writing Example
with open("data.txt", "a") as file:
file.write("Safe append operation\n")Conclusion
Writing to files in Python is essential for storing and managing data.
You learned:
- How to write, append, and create files
- Difference between
w,a, andxmodes - Writing multiple lines and user input
- Safe file handling using
with
Mastering file writing helps you build real-world applications like logs, notes apps, and data storage systems.


0 Comments