Back to Python
2025-12-115 min read

file operations (Python Programming)

Learn file operations (Python Programming) step by step with clear examples and exercises.

Title: Mastering Python File Operations: A full guide

Why This Matters

In programming, handling files is crucial for data storage, retrieval, and manipulation. Python offers a rich set of built-in functions to work with files, making it an excellent choice for beginners and experts alike. Understanding file operations can help you create efficient scripts, solve real-world problems, and even ace your programming interviews.

Prerequisites

Before diving into the core concept, ensure you have a solid understanding of:

  1. Python basics (variables, data types, operators)
  2. Control structures (if-else statements, loops)
  3. Basic file I/O functions (print(), input())
  4. Understanding of exceptions and error handling
  5. Familiarity with list comprehensions and generator expressions

Core Concept

Python provides several built-in functions to read from and write to files. Let's explore some essential functions:

  1. open(filename, mode) - Opens a file with the specified filename and mode. Modes include 'r' (read), 'w' (write), 'a' (append), 'x' (create new), 'b' (binary), and more.
  2. read() - Reads the entire content of the file as a single string.
  3. readline() - Reads one line from the file.
  4. readlines() - Reads all lines in the file as a list of strings.
  5. write(str) - Writes the provided string to the file.
  6. writelines(iterable) - Writes each item in the iterable (e.g., a list of strings) to the file on separate lines.
  7. seek(offset, from_what=0) - Moves the file pointer to a specific position in the file.
  8. tell() - Returns the current position of the file pointer.
  9. truncate([size]) - Truncates or resizes the file to a specified size (optional).
  10. close() - Closes the file after you're done working with it.

File Modes

  • 'r' - Read mode: Opens the file for reading only. The default file position is at the beginning of the file.
  • 'w' - Write mode: Opens the file for writing only. If the file already exists, its content will be erased. If it doesn't exist, a new file will be created.
  • 'a' - Append mode: Opens the file for appending data at the end of the file. If the file doesn't exist, a new one will be created.
  • 'x' - Create mode: Opens the file only if it does not exist; raises an error if it already exists.
  • 'b' - Binary mode: Opens the file for binary data instead of text.

Worked Example

Let's create a simple Python script that reads lines from a file, counts the number of words, and writes the result to another file:

def count_words(filename):
with open(filename, 'r') as f:
words = f.read().split()
return len(words)

with open('input.txt', 'r') as f_in, open('output.txt', 'w') as f_out:
num_words = count_words('input.txt')
f_out.write(f'Number of words in input.txt: {num_words}')

In this example, we use a context manager (with statement) to automatically open and close the files, ensuring that resources are properly released after use. We also demonstrate the use of list comprehensions for splitting the file content into words.

Common Mistakes

  1. Forgetting to close the file: Always remember to call f.close() or use a context manager like with.
  2. Not specifying the mode while opening the file: Make sure you open the file in the appropriate mode (read, write, append).
  3. Writing to the wrong file: Ensure you're writing to the correct output file.
  4. Ignoring exceptions: Always handle potential errors using try-except blocks when working with files.
  5. Not accounting for empty lines or whitespace: Be aware of how these can affect line and word counts.
  6. Using read() on large files without proper memory management: Use readlines(), iterate through the lines, or use generators to handle large files efficiently.
  7. Forgetting to seek back to the beginning of the file when writing multiple times: Use f.seek(0) before writing again to ensure you're not overwriting the previous content.

Practice Questions

  1. Write a script that reads the first 5 lines of a file and prints them.
  2. Write a script that counts the number of words in a file, excluding punctuation.
  3. Write a script that appends a list of names to a file, one per line, sorted alphabetically.
  4. Write a script that replaces all occurrences of 'old_text' with 'new_text' in a file, case-insensitively.
  5. Write a script that reads a CSV file and calculates the sum of a specific column.
  6. Write a script that reads a binary file and reverses its contents.
  7. Write a script that reads a large text file line by line, counting the number of unique words in memory-efficient manner.
  8. Write a script that merges two sorted text files into one, maintaining their order.
  9. Write a script that finds and removes duplicate lines from a file.
  10. Write a script that compresses a text file by removing all empty lines and whitespace characters except for newlines.

FAQ

Q: What happens if I open a file in write mode and it already exists?

A: The existing content will be erased, and the file will be rewritten from scratch.

Q: Can I read and write to the same file simultaneously using Python?

A: Yes, but with caution. Use locking mechanisms or concurrent programming techniques to avoid conflicts.

Q: How can I create a new file if it doesn't exist when opening in 'a' mode?

A: You can use the open(filename, 'x') function to create a new file that you can open in append mode later on.

Q: How do I handle errors like FileNotFoundError or PermissionError?

A: Wrap your file operations inside a try-except block and catch these specific exceptions to handle errors gracefully.

Q: How can I read and write binary files efficiently in Python?

A: Use the open(filename, 'rb') or open(filename, 'wb') modes for reading and writing binary data, respectively. For large files, consider using generators or chunked reads/writes to manage memory usage.

file operations (Python Programming) | Python | XQA Learn