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 writelines Function - Complete Guide Python writelines FunctionLast modified March 26, 2025
This comprehensive guide explores Python's Basic Definition
The
This method is particularly useful when you have a list of strings to write.
It's more efficient than multiple Basic writelines Example
This example demonstrates the simplest use of basic_writelines.py
lines = ['First line\n', 'Second line\n', 'Third line\n']
with open('output.txt', 'w') as file:
file.writelines(lines)
This code creates or overwrites 'output.txt' and writes three lines. Each string
in the list includes a newline character. Without these, all text would appear
on one line. The
The Writing Without Explicit NewlinesThis example shows what happens when you omit newline characters. The strings are written consecutively without separation. no_newlines.py
items = ['apple', 'banana', 'cherry']
with open('fruits.txt', 'w') as file:
file.writelines(items)
The resulting file will contain "applebananacherry" without any spaces or
newlines. This demonstrates that To add separators, you could use a generator expression with newlines or map the list to add newlines. This behavior differs from some other languages' similar methods which might add newlines automatically. Using Generator ExpressionsGenerator expressions can efficiently transform data before writing. This example adds line numbers to each line during writing. generator_writelines.py
data = ['red', 'green', 'blue']
with open('colors.txt', 'w') as file:
file.writelines(f"{i+1}: {color}\n" for i, color in enumerate(data))
This code writes numbered lines to the file. The generator expression creates formatted strings on the fly without creating an intermediate list. This is memory-efficient for large datasets.
The f-string formats each line with its index (starting at 1) and value. The
Writing from a File to Another
This example reads lines from one file and writes them to another using
file_copy_writelines.py
with open('source.txt', 'r') as source:
with open('destination.txt', 'w') as dest:
dest.writelines(source)
This code copies 'source.txt' to 'destination.txt'. The file object is an
iterator yielding lines, which Note that this preserves the original line endings. For binary data or precise copying, use binary mode. This pattern works well for line-oriented text processing and transformations between files. Filtering Before WritingThis example demonstrates filtering lines before writing. Only lines containing 'a' are written to the output file. filter_writelines.py
lines = ['apple\n', 'banana\n', 'cherry\n', 'date\n']
with open('filtered.txt', 'w') as file:
file.writelines(line for line in lines if 'a' in line)
The output file will contain only 'apple' and 'banana' lines. The generator expression filters the sequence before writing. This pattern is powerful for selective output without temporary lists. You can combine multiple conditions in the filter. This approach is memory- efficient for large datasets as it processes items one at a time without storing the entire filtered collection. 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. |