Explain Codes LogoExplain Codes Logo

Writing a list to a file with Python, with newlines

python
file-handling
context-managers
json
Nikita BarsukovbyNikita Barsukov·Aug 13, 2024
TLDR

The fast-track to writing a list to a file with each element as a new line is to combine list items using '\n'.join() and then write() to the file:

lines = ['First', 'Second', 'Third'] # Open the file with open('file.txt', 'w') as file: # Write each list item to a new line file.write('\n'.join(lines))

Writing methods and best practices

Use context managers for safer file handling

By wrapping the file handling logic block with a context manager (with statement), we ensure the file is automatically closed after operation, whether it was successful or error-prone. Otherwise, the file might be black and blue!

# Ensure file will be closed after operation with open('file.txt', 'w') as file: file.write("Safely written str.\n")

Conditional writing with loops

Sometimes you're the bouncer at text club and you need to decide who gets in. Using a loop allows adding conditional checks before writing to the file:

lines = ['data: First', 'error: Second', 'data: Third'] with open('log.txt', 'w') as file: for line in lines: # Only write data lines, no errors allowed! if 'error:' not in line: file.write(f"{line}\n")

Utilize efficient f-strings for line writing

Let's say, you'd like to write about your friend, Bob. F-strings allow efficient string formatting:

name, age = 'Bob', 28 with open('info.txt', 'w') as file: # Files love gossip about Bob file.write(f"Name: {name}\nAge: {age}\n")

Writing large lists without a memory hog

Picture your list as a buffet. A generator expression avoids piling all the data on your plate (memory) at once:

# Buffet-style iteration large_data = (str(num) for num in range(1000000)) with open('large_file.txt', 'w') as file: file.write('\n'.join(large_data))

Alternatives for vintage Python

For older Python versions, like that retro jacket you love, the % operator or the format() method can be used for string formatting:

lines = ['First', 'Second', 'Third'] with open('file.txt', 'w') as file: for line in lines: # Retro style writing file.write("%s\n" % line)

Use serialization to preserve more complex data types

Sometimes, simple strings and numbers are not enough. You've got dictionaries or custom objects to store. You need pickle:

import pickle data = {'key': 'value', 'list': [1, 2, 3]} # Store in a pickle jar with open('data.pkl', 'wb') as file: pickle.dump(data, file)

Opt for JSON when sharing is caring

When you'd like to be cross-language compatible and readable to humans, json.dump() writes your list into a JSON format:

import json data = ['First', 'Second', 'Third'] # Let's make this pretty... with open('data.json', 'w') as file: json.dump(data, file)

Handling big lists

Efficiently write in chunks

When dealing with lists that are so large they might devour your memory, you can process and write in chunks:

def write_in_chunks(data, file_path, chunk_size=1024): with open(file_path, 'w') as file: # Chunk by chunk! Nom nom... for i in range(0, len(data), chunk_size): file.write('\n'.join(data[i:i+chunk_size]) + '\n')

Safeguard complex data with binary mode

Like a nightclub for data, you can only get in if you've got the right tag. When storing complex data structures with pickle, use the 'binary mode' to ensure proper data handling:

import pickle complex_structure = { "It's": "Complicated"} # Keep it safe. Use bouncers. with open('large_data.pkl', 'wb') as file: pickle.dump(complex_structure, file)

Structure and readability with JSON

JSON.dump() serves data right in a structured JSON format, readable and analyzable without proprietary tools. You know, the kind of format you'd take home to introduce to your parents:

import json large_data = [dict(num=i, square=i**2) for i in range(1000)] # JSON: Always neat, structured and polite with open('large_data.json', 'w') as file: json.dump(large_data, file, indent=4)

Special cases

Custom sorting of the list

Fashion your list to the party theme! Python makes it easy to sort your list by a custom key function before writing:

tasks = ['Write code', 'Test', 'Debug', 'Deploy'] # Sort by length of task description tasks.sort(key=lambda x: len(x)) with open('sorted_tasks.txt', 'w') as file: file.writelines('\n'.join(tasks))

Filtering elements from the list

When you have unwanted party crashers, filter out the undesired entries using a list comprehension:

errors = ['Error 1', 'Detail 2', 'Error 3'] with open('errors.txt', 'w') as file: # Only the cool kids no "Error" file.writelines('\n'.join(e for e in errors if 'Error' not in e))