Skip to content

Instantly share code, notes, and snippets.

@abraham-ny
Created May 13, 2025 17:31
Show Gist options
  • Select an option

  • Save abraham-ny/6a2404435ab10cfee7609f9efeb1b546 to your computer and use it in GitHub Desktop.

Select an option

Save abraham-ny/6a2404435ab10cfee7609f9efeb1b546 to your computer and use it in GitHub Desktop.
PLP-Python-FileHandling Assignment
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