Hone logo
Hone
Problems

Mastering File I/O in Python: A Text Transformation Challenge

This challenge will guide you through the fundamental concepts of reading from and writing to files in Python. You'll create a program that processes a text file, modifies its content, and saves the changes to a new file. This skill is essential for any application that needs to store or retrieve data persistently.

Problem Description

Your task is to build a Python script that performs the following operations:

  1. Read the content of an input text file.
  2. Process the content: For every line read, replace all occurrences of the word "Python" with "CodeMaster". Case should be ignored during the replacement (e.g., "python", "Python", "PYTHON" should all be replaced).
  3. Write the modified content to a new output text file.

Key Requirements:

  • The script should accept two command-line arguments: the path to the input file and the path to the output file.
  • The original input file should remain unchanged.
  • The output file should contain the transformed text.
  • Handle potential FileNotFoundError if the input file does not exist.

Expected Behavior:

The script should gracefully handle the creation of the output file and overwrite it if it already exists. If the input file is not found, it should print an informative error message and exit.

Edge Cases to Consider:

  • An empty input file.
  • An input file with lines containing only the word "Python" (case-insensitive).
  • An input file where "Python" appears multiple times on a single line.
  • An input file with special characters or extended ASCII characters.

Examples

Example 1:

Input File (input.txt):

This is a sample text file.
Python is a versatile programming language.
I love learning Python!
PYTHON is great.

Command: python your_script_name.py input.txt output.txt

Output File (output.txt):

This is a sample text file.
CodeMaster is a versatile programming language.
I love learning CodeMaster!
CodeMaster is great.

Explanation: The script read input.txt, replaced all instances of "Python" (case-insensitive) with "CodeMaster", and saved the result to output.txt.

Example 2:

Input File (empty_input.txt):

Command: python your_script_name.py empty_input.txt empty_output.txt

Output File (empty_output.txt):

Explanation: An empty input file results in an empty output file.

Example 3:

Input File (no_python.txt):

This file does not contain the target word.
It should be copied as is.

Command: python your_script_name.py no_python.txt no_python_output.txt

Output File (no_python_output.txt):

This file does not contain the target word.
It should be copied as is.

Explanation: If the target word is not present, the file content is copied without modification.

Constraints

  • Input and output file paths will be valid strings.
  • The maximum size of the input file is assumed to be within reasonable memory limits for typical text files (e.g., less than 100MB).
  • The script should be written to be efficient for processing lines sequentially.

Notes

  • Consider using the with open(...) statement for robust file handling, as it ensures files are automatically closed even if errors occur.
  • Python's string manipulation methods will be very helpful here, particularly for finding and replacing substrings. Remember to handle case-insensitivity during the replacement.
  • The sys module can be used to access command-line arguments.
Loading editor...
python