Created
May 13, 2025 17:31
-
-
Save abraham-ny/6a2404435ab10cfee7609f9efeb1b546 to your computer and use it in GitHub Desktop.
PLP-Python-FileHandling Assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def read_and_write_file(): | |
| filename = input("Enter the filename to read: ") | |
| # Attempt to open the file and read its contents | |
| try: | |
| with open(filename, 'r') as file: | |
| content = file.read() | |
| print("\nOriginal Content:") | |
| print(content) | |
| # Modify the content (for example, converting to uppercase) | |
| modified_content = content.upper() | |
| # Create a new file to write the modified content | |
| new_filename = "modified_" + filename | |
| with open(new_filename, 'w') as new_file: | |
| new_file.write(modified_content) | |
| print(f"\nModified content has been written to '{new_filename}' successfully.") | |
| # Handle the case where the file doesn't exist | |
| except FileNotFoundError: | |
| print(f"Error: The file '{filename}' was not found. Please check the filename and try again.") | |
| # Handle the case where the file cannot be read | |
| except IOError: | |
| print(f"Error: Unable to read the file '{filename}'. Please check your permissions or file status.") | |
| read_and_write_file() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment