Week 4 assignment on Python Programming def read_and_modify_file(): try: # Ask the user for the filename filename = input("Enter the filename to read: ")
# Open the file for reading
with open(filename, "r") as file:
content = file.read() # Read the file content
# Modify the content (convert to uppercase)
modified_content = content.upper()
# Ensure the file has a .txt extension before creating a new filename
if filename.endswith(".txt"):
new_filename = filename.replace(".txt", "_modified.txt")
else:
new_filename = filename + "_modified.txt"
# Write the modified content to a new file
with open(new_filename, "w") as file:
file.write(modified_content)
print(f"Modified content written to: {new_filename}")
except FileNotFoundError:
print("Error: The file does not exist. Please check the filename.")
except PermissionError:
print("Error: You do not have permission to read this file.")
except Exception as e:
print(f"Unexpected error: {e}")
read_and_modify_file()