-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_finally.py
More file actions
28 lines (20 loc) · 822 Bytes
/
try_finally.py
File metadata and controls
28 lines (20 loc) · 822 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
27
28
# Task 1: The Secret Message
sent_message = "Hey there! This is a secret message."
# Save the sent message to a file
with open('sent_message.txt', 'w') as file:
file.write(sent_message)
# Task 2: Simulate Unsending the Message
with open('sent_message.txt', 'r+') as file:
# Read the sent message from the file
original_message = file.read()
# Move the cursor to the beginning of the file
file.seek(0)
# Modify the message to simulate unsending
unsent_message = "This message has been unsent."
# Truncate the file to the length of the modified message
file.truncate(len(unsent_message))
# Write the modified message to the file
file.write(unsent_message)
# Display the modified message
print("Original Message:", original_message)
print("Unsent Message:", unsent_message)