Skip to content

Francis-2030/Week-4-Python-Programming-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Week-4-Python-Programming-

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}")

Run the function

read_and_modify_file()

About

Week 4 assignment on Python Programming

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published