-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_modifier.py
More file actions
26 lines (19 loc) · 868 Bytes
/
file_modifier.py
File metadata and controls
26 lines (19 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def read_and_modify_file(filename):
try:
with open(filename, 'r') as file:
content = file.readlines()
# Modification: add a "[MODIFIED] " prefix to each line
modified_content = [f"[MODIFIED] {line}" for line in content]
new_filename = f"modified_{filename}"
with open(new_filename, 'w') as new_file:
new_file.writelines(modified_content)
print(f"✅ File modified successfully! New file created: {new_filename}")
except FileNotFoundError:
print("❌ Error: File not found.")
except PermissionError:
print("❌ Error: You don't have permission to read/write this file.")
except Exception as e:
print(f"❌ An unexpected error occurred: {e}")
# Main program
filename = input("📎 Enter the name of the file to read: ")
read_and_modify_file(filename)