Hone logo
Hone
Problems

File I/O Mastery: Reading and Writing Text Files

This challenge focuses on fundamental file input/output (I/O) operations in Python. Being able to read data from and write data to files is a crucial skill for any programmer, enabling programs to persist data and interact with external resources. Your task is to create a Python script that reads data from an input file, modifies it, and writes the modified data to an output file.

Problem Description

You are tasked with creating a Python script that performs the following actions:

  1. Reads from an Input File: The script should accept the path to an input text file as a command-line argument. The input file will contain a series of lines of text.
  2. Transforms the Data: For each line read from the input file, the script should convert the line to uppercase.
  3. Writes to an Output File: The script should accept the path to an output text file as a second command-line argument. The transformed (uppercase) lines should be written to this output file, one line per line.
  4. Handles File Not Found Errors: If the input file does not exist, the script should print an informative error message to the console and exit gracefully (without attempting to write to the output file).
  5. Handles Write Errors: If there's an error writing to the output file (e.g., permission denied), the script should print an informative error message to the console and exit gracefully.

Expected Behavior:

The script should take two command-line arguments: the input file path and the output file path. It should read the input file line by line, convert each line to uppercase, and write the uppercase version to the output file. If any errors occur during file operations, appropriate error messages should be printed to the console.

Examples

Example 1:

Input File (input.txt):
hello world
this is a test
python is fun
Output File (output.txt):
HELLO WORLD
THIS IS A TEST
PYTHON IS FUN

Explanation: The script reads each line from input.txt, converts it to uppercase, and writes the result to output.txt.

Example 2:

Input:  python your_script.py non_existent_file.txt output.txt
Output: Error: Input file 'non_existent_file.txt' not found.

Explanation: The input file non_existent_file.txt does not exist. The script prints an error message and exits. No output file is created.

Example 3:

Input: python your_script.py input.txt /path/to/protected/output.txt
Output: Error: Could not write to output file '/path/to/protected/output.txt'. Permission denied.

Explanation: The script attempts to write to a file where it lacks the necessary permissions. An error message is printed, and the script exits.

Constraints

  • The input file will contain only text.
  • The output file will contain only text.
  • The input and output file paths will be strings.
  • The script must handle file not found errors and write errors gracefully.
  • The script must accept the input and output file paths as command-line arguments.
  • The script should be reasonably efficient for files up to 10MB in size.

Notes

  • Consider using the try...except block to handle potential file I/O errors.
  • The open() function is your primary tool for file operations. Remember to use the appropriate modes ('r' for reading, 'w' for writing).
  • The with open(...) as file: construct is recommended for automatic file closing, even in case of errors.
  • Use sys.argv to access command-line arguments. Remember that sys.argv[0] is the script name itself.
  • Think about how to provide clear and informative error messages to the user.
Loading editor...
python