ZetCode

Python write Function

Last modified March 26, 2025

This comprehensive guide explores Python's write function, the primary method for writing data to files in Python. We'll cover basic usage, file modes, context managers, encoding handling, and best practices.

Basic Definitions

The write function is a method of file objects in Python used to write data to files. It takes a string (or bytes in binary mode) as input and writes it to the file at the current position.

Key characteristics of write: it doesn't automatically add newlines, returns the number of characters/bytes written, and may buffer output until flush or close is called.

Basic File Writing

The simplest use of write writes a string to a file opened in write ('w') or append ('a') mode. Write mode creates or overwrites files.

basic_write.py
# Open a file for writing
file = open('output.txt', 'w')
file.write('Hello, Python!\n')
file.write('This is line two.\n')
file.close()

This example creates 'output.txt' (or overwrites if it exists) and writes two lines. Note the explicit \n newline characters. The close call ensures all data is written to disk.

The write method returns the number of characters written (in text mode) or bytes written (in binary mode). This can be useful for verification.

Writing Multiple Lines

For writing multiple lines efficiently, you can use writelines or loop through a list of strings with write.

multi_line_write.py
# Writing multiple lines efficiently
lines = ['First line\n', 'Second line\n', 'Third line\n']

with open('multi.txt', 'w') as file:
    file.writelines(lines)
    # Or alternatively:
    # for line in lines:
    #     file.write(line)

This example demonstrates two approaches to write multiple lines. The writelines method writes all lines at once, while the loop provides more control. Note that writelines doesn't add newlines.

The with statement ensures proper file closure. This is especially important when writing files to ensure all data is flushed to disk.

Appending to Files

To add content without overwriting existing data, open the file in append ('a') mode. The write operation will always add to the end.

append_write.py
# Appending to an existing file
with open('log.txt', 'a') as file:
    file.write('New log entry at: ')
    file.write('2025-03-26 14:30:00\n')

This code opens 'log.txt' in append mode and adds a timestamped log entry. If the file doesn't exist, it will be created. Each run appends new content.

Append mode is commonly used for logging, data collection, or any scenario where you need to preserve historical data while adding new information.

Binary File Writing

For non-text files, use binary mode ('wb'). In this mode, write accepts bytes objects instead of strings.

binary_write.py
# Writing binary data
data = bytes([0x48, 0x65, 0x6c, 0x6c, 0x6f])  # 'Hello' in ASCII

with open('binary.bin', 'wb') as file:
    bytes_written = file.write(data)
    print(f"Bytes written: {bytes_written}")

This example creates a binary file and writes a sequence of bytes representing 'Hello' in ASCII. The write method returns the number of bytes written, which we print for verification.

Binary mode is essential for images, executables, or any file where byte- for-byte accuracy is required. Text mode would corrupt such files.

Writing with Encoding

When writing text files, you can specify character encoding to ensure proper handling of non-ASCII characters.

encoding_write.py
# Writing with specific encoding
text = "Résumé - 简历 - Lebenslauf"

with open('multilingual.txt', 'w', encoding='utf-8') as file:
    file.write(text)

This code writes a string containing multiple languages to a UTF-8 encoded file. UTF-8 can represent all Unicode characters and is the recommended encoding for text files in most cases.

If you don't specify encoding, Python uses the default system encoding, which might not handle all characters correctly. Always specify encoding for portable code.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.