-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 4.py
More file actions
44 lines (31 loc) · 1.17 KB
/
Assignment 4.py
File metadata and controls
44 lines (31 loc) · 1.17 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Task 1: Read a File and Handle Errors
def read_file():
file_name = "sample.txt"
try:
with open(file_name, "r") as file:
print("Reading file content:")
for index, line in enumerate(file, start=1):
print(f"Line {index}: {line.strip()}")
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
if __name__ == "__main__":
read_file()
Task 2: Write and Append Data to a File
def write_and_append_file():
file_name = "output.txt"
try:
user_input = input("Enter text to write to the file: ")
with open(file_name, "w") as file:
file.write(user_input + "\n")
print("Data successfully written to output.txt.")
additional_input = input("Enter additional text to append: ")
with open(file_name, "a") as file:
file.write(additional_input + "\n")
print("Data successfully appended.")
print("\nFinal content of output.txt:")
with open(file_name, "r") as file:
print(file.read())
except Exception as e:
print("An error occurred:", e)
if __name__ == "__main__":
write_and_append_file()