Reading and Writing Text Files

Summary:
Read from and write to files in your scripts.


Working with files is a fundamental task in programming. Reading and writing text files allow your scripts to store data, process information, and exchange data with other programs. In this article, we'll explore how to work with text files in Python, covering basic file operations, common pitfalls, and useful tips for effective file handling.


Why Read and Write Files?

Scripts often need to persist data between runs, read configurations, log results, or process external information. Text files are one of the most straightforward and portable formats for such tasks.

Common use cases:

  • Reading configuration files.
  • Logging messages or results.
  • Processing input data (CSV, logs, etc.).
  • Generating reports or documents.

Opening Files in Python

In Python, files are opened using the built-in open() function. The basic syntax is:

file_object = open('filename.txt', 'mode')
  • 'filename.txt' is the name of your file.
  • 'mode' specifies what you want to do with the file:
    • 'r': Read (default)
    • 'w': Write (creates/truncates file)
    • 'a': Append (writes at end)
    • 'b': Binary mode
    • '+': Read and write

Example:

f = open('data.txt', 'r')

Reading from a File

There are several ways to read text from a file:

1. Read the Entire File

with open('data.txt', 'r') as f:
    content = f.read()
print(content)

2. Read Line by Line

with open('data.txt', 'r') as f:
    for line in f:
        print(line.strip())

3. Read All Lines into a List

with open('data.txt', 'r') as f:
    lines = f.readlines()
print(lines)

Writing to a File

When writing, opening with 'w' will overwrite the file, while 'a' will append.

1. Write a String

with open('output.txt', 'w') as f:
    f.write('Hello, world!\n')

2. Write Multiple Lines

lines = ['First line\n', 'Second line\n', 'Third line\n']
with open('output.txt', 'w') as f:
    f.writelines(lines)

Using "with" Statement

Notice the use of with above. This context manager ensures the file is properly closed after use, even if an error occurs.

with open('filename.txt', 'mode') as f:
    # File operations
# File is automatically closed here

Handling Exceptions

File operations can fail (e.g., file not found, permission denied). Handle exceptions as needed:

try:
    with open('config.txt', 'r') as f:
        config = f.read()
except FileNotFoundError:
    print('Configuration file not found!')
except Exception as e:
    print('An error occurred:', e)

Tips and Best Practices

  • Always close your files (use the with statement).
  • Check file existence before reading/writing if needed.
  • Use absolute paths or understand your working directory.
  • Handle exceptions gracefully.
  • Consider using libraries like csv or json for structured data.

When to Use Other Formats

Plain text files are versatile, but for structured data, consider:

  • csv for comma-separated values.
  • json for structured hierarchical data.
  • pickle for Python object serialization (not human-readable).

Conclusion

Reading and writing text files in your scripts is a vital and accessible technique. Whether you're processing logs, storing user input, or automating configuration management, file handling helps your programs become more robust and versatile. With Python's intuitive syntax and best practices like the with statement, working with files is simple and effective.


Happy scripting!