Back to Python
2026-01-027 min read

Python - Write to File

Learn Python - Write to File step by step with clear examples and exercises.

Why This Matters

Writing data to a file is an essential skill for every Python programmer. It allows you to store, retrieve, and manipulate data persistently outside your code. This feature is crucial in various real-world scenarios such as logging user activities, saving game progress, or creating reports. In interviews, the ability to write to files demonstrates your understanding of Python's I/O operations and can help you solve complex problems.

Importance of Writing to Files:

  1. Persistence: Data written to a file remains even after the program terminates, allowing for long-term storage and retrieval.
  2. Efficiency: Writing large amounts of data to memory can be inefficient due to limitations on available RAM. Storing this data in files allows you to work with manageable chunks.
  3. Sharing Data: Files can be easily shared between different programs, platforms, or even users.
  4. Logging and Debugging: Writing logs can help diagnose issues during development and production phases, making it easier to understand the behavior of your application.

Prerequisites

Before diving into writing to a file in Python, it is essential that you have a good grasp of:

  • Basic Python syntax and data types
  • Understanding variables, strings, and lists
  • Control structures like loops and conditional statements
  • Familiarity with the concept of files and directories
  • Knowledge of exception handling for error management

Core Concept

Python provides several built-in functions for handling files. The most common ones are open(), write(), read(), close(), and flush().

Opening a File

To open a file in Python, you use the open() function. This function takes two arguments: the name of the file and the mode in which you want to open it. Here's an example of opening a file in write mode (w) to create a new file:

file = open('example.txt', 'w')

In addition to 'r', 'w', and 'a', other modes include:

  • 'x': Creates the file only if it doesn't exist; raises an error if the file already exists.
  • 'rb', 'wb', 'ab': Open the file in binary mode for reading or writing.
  • '+r', '+w', '+a': Open the file for both reading and writing, with read access by default.

Writing to a File

Once you have opened the file, you can use the write() function to write data into it. The write() function takes a string as an argument and appends it to the end of the file:

file.write('Hello, World!')

Reading from a File

To read the contents of a file, you can use the read() function. This function reads all the data in the file as a single string:

contents = file.read()
print(contents)

You can also read the file line by line using a loop and the readline() function:

for line in file:
print(line, end='')

Closing a File

After you're done writing to or reading from a file, don't forget to close it using the close() function. This ensures that any changes are saved and resources are released:

file.close()

Writing to an Existing File

If you want to append data to an existing file instead of overwriting it, open the file in append mode (a) like so:

file = open('example.txt', 'a')
file.write('\nNew Line')
file.close()

Flushing a File

To ensure that all data is written to the file immediately, you can use the flush() method:

file.flush()
file.close()

Error Handling

You should always handle exceptions when working with files. The open() function can raise an IOError if it encounters problems like file not found or permission denied. Here's how to handle such errors:

try:
file = open('example.txt', 'w')
except IOError as e:
print(f"Error opening the file: {e}")

Worked Example

Let's create a simple Python script that writes some data to a file, reads it back, and then appends more data:

try:
file = open('example.txt', 'w')
except IOError as e:
print(f"Error opening the file: {e}")
exit()

file.write('Hello, World!\n')
file.write('Today is: ')
file.write(str(datetime.date.today()) + '\n')
file.close()

try:
file = open('example.txt', 'r')
except IOError as e:
print(f"Error opening the file: {e}")
exit()

contents = file.read()
print(contents)
file.close()

try:
file = open('example.txt', 'a')
except IOError as e:
print(f"Error opening the file: {e}")
exit()

file.write('\nThis is an update!')
file.close()

Common Mistakes

  1. Forgetting to close the file after writing data to it can lead to resource leaks and potential errors.
  2. Writing to a file without opening it in write or append mode will result in an error.
  3. Not specifying the file path correctly can cause the file to not be found, leading to issues when trying to read or write data.
  4. If you open a file in write mode (w) and try to read from it immediately, you'll encounter an empty file because the cursor is at the end of the file after opening.
  5. Writing to a file without flushing the buffer can cause data loss if the program crashes or the system experiences an unexpected shutdown. To avoid this, use the flush() method before closing the file:
file.flush()
file.close()
  1. Not handling exceptions properly when working with files can lead to unhandled errors and program crashes.
  2. Forgetting to check if a file exists before trying to open it in write or append mode can cause issues, especially when dealing with user-supplied filenames.
  3. Using the wrong mode (e.g., opening a binary file in text mode) can lead to unexpected results and data corruption.
  4. Failing to properly handle encoding when working with files containing non-ASCII characters can result in incorrect data being written or read.
  5. Not using context managers like with open() as file ensures that the file is automatically closed after the block of code, reducing the chance of forgetting to close it manually.

Practice Questions

  1. Write a Python script that creates a new text file named "data.txt" and writes the string "Welcome to my data file!" to it.
  2. Modify the previous script so that it appends the string "This is an update!" to the end of the existing "data.txt" file instead of overwriting it.
  3. Write a Python script that takes a list of names as input and writes each name to a separate line in a new file named "names.txt".
  4. Write a Python script that reads the contents of a file named "example.txt", splits the content into lines, and prints each line.
  5. Write a Python script that opens a file named "example.txt" in append mode, writes the current date and time to the file, and then closes the file.
  6. Write a Python script that reads a CSV file containing student names and grades, calculates the average grade, and writes the result to a new file.
  7. Write a Python script that opens a binary file in write mode, writes an image (e.g., PNG or JPEG) to it, and then closes the file.
  8. Write a Python script that reads a text file line by line, counts the number of occurrences of each word, and writes the results to a new file sorted by frequency.
  9. Write a Python script that opens a log file in append mode, appends a timestamped log message, and then closes the file. The log message should include the current date and time, process ID, and a custom message.
  10. Write a Python script that opens a compressed archive (e.g., ZIP or TAR) in read mode, extracts its contents to a specified directory, and then closes the archive.

FAQ

Q: What happens if I try to write to a file that doesn't exist?

A: If you attempt to write to a file that does not exist, Python will create the file for you. However, if you open the file in write mode (w), it will overwrite any existing content. To append data to an existing file, use a mode instead.

Q: How can I read the contents of a file after writing to it?

A: You can read the contents of a file using the built-in read() function. Here's an example:

file = open('example.txt', 'r')
contents = file.read()
print(contents)
file.close()

Q: What is the difference between opening a file in read mode (r) and binary read mode (rb)?

A: In Python, r opens a file for reading text, while rb opens it for reading binary data. Text files can contain characters that are not valid in binary data, such as newline characters (\n), which are represented as two bytes (\x0a\x0d) on Windows systems. Opening a text file in binary mode ensures that these characters are read and written correctly across different operating systems.

Q: How can I write to a file using multiple threads without overwriting each other's data?

A: To write to a file using multiple threads without overwriting each other's data, you can use the locking mechanism provided by Python's built-in threading module. Each thread will have its own lock, and only one thread can acquire the lock at a time, preventing simultaneous writes. Here's an example:

import threading

file = open('example.txt', 'a')
lock = threading.Lock()

def writer(name):
with lock:
file.write(f'{name}\n')

threads = [threading.Thread(target=writer, args=(name,)) for name in ['Alice', 'Bob', 'Charlie']]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
file.close()
Python - Write to File | Python | XQA Learn