diff --git a/2-file-and-error-handling/08_uncharted_files.py b/2-file-and-error-handling/08_uncharted_files.py index e72766c..d280241 100644 --- a/2-file-and-error-handling/08_uncharted_files.py +++ b/2-file-and-error-handling/08_uncharted_files.py @@ -1,30 +1,30 @@ -# Uncharted Files 📂 -# Codédex +# 1. Create the file and write the original secret message +sent_message = 'Hey there! This is a secret message.' -# 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 +# 2. The "Unsend" Process: Read, seek, overwrite, and truncate 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 + + # Move the cursor back to the beginning (0) so we can overwrite 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 + unsent_message = 'This message has been unsent.' file.write(unsent_message) + + # Use .truncate() to chop off any leftover characters from the old message + file.truncate(len(unsent_message)) + + # 3. The Verification: Move the cursor back to the beginning ONE MORE TIME + file.seek(0) + + # Read exactly what is currently inside the file + final_file_content = file.read() -# Display the modified message -print("Original Message:", original_message) -print("Unsent Message:", unsent_message) +# 4. Print the results! +print("What the file used to say:", original_message) +print("What the file says now:", final_file_content)