Python:
Writing a text file

How to:

Using Built-In open() Function

Python’s built-in open() function is the most common way to write to files. The function allows for specifying the mode in which the file is opened - ‘w’ for write (overwriting), ‘a’ for append, and ‘w+’ for write+read.

# Writing to a new file or replacing an existing file
with open('example.txt', 'w') as file:
    file.write("Hello, World!\n")

# Appending to a file
with open('example.txt', 'a') as file:
    file.write("Appending more text.\n")

# Reading the file to verify
with open('example.txt', 'r') as file:
    print(file.read())

Sample Output:

Hello, World!
Appending more text.

Using pathlib.Path

For a more object-oriented approach, the Path class from the pathlib module offers a method to write to files. This is a popular method for newer Python codebases.

from pathlib import Path

# Writing/Replacing a file
Path('example2.txt').write_text("This is example 2.\n")

# Reading the file to verify
print(Path('example2.txt').read_text())

# Note: `Path.write_text` always overwrites the file content. 
# For appending, you'll need to open the file as shown in the previous section.

Sample Output:

This is example 2.

Third-party Libraries

For complex file operations, third-party libraries like pandas (for CSV, Excel files) can be a great asset. Here’s a quick example of writing a DataFrame to a CSV file using pandas, demonstrating its utility beyond simple text files.

# This example requires pandas: pip install pandas
import pandas as pd

# Creating a simple DataFrame
data = pd.DataFrame({'Column1': [1, 2, 3], 'Column2': ['A', 'B', 'C']})

# Writing DataFrame to a CSV file
data.to_csv('example.csv', index=False)

# Reading the CSV to verify
print(pd.read_csv('example.csv'))

Sample Output:

   Column1 Column2
0        1       A
1        2       B
2        3       C

Using these methods, Python programmers can effectively manage file operations, catering to both simple and complex data handling needs.