GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python write Function - Complete Guide Python write FunctionLast modified March 26, 2025
This comprehensive guide explores Python's Basic Definitions
The
Key characteristics of Basic File Writing
The simplest use of 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
The Writing Multiple Lines
For writing multiple lines efficiently, you can use 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
The Appending to Files
To add content without overwriting existing data, open the file in append ('a')
mode. The 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, 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 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 EncodingWhen 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 ReferencesAuthorMy 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. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |