-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path07_file_read_write.py
More file actions
29 lines (24 loc) · 885 Bytes
/
Copy path07_file_read_write.py
File metadata and controls
29 lines (24 loc) · 885 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
29
# Demonstrating basic file operations (Reading and Writing)
file_name = "day_07_notes.txt"
def write_to_file(text):
# 'w' mode overwrites. 'a' mode appends.
with open(file_name, "w") as file:
file.write(text + "\n")
print(f"Successfully wrote to {file_name}")
def append_to_file(text):
with open(file_name, "a") as file:
file.write(text + "\n")
print(f"Successfully appended to {file_name}")
def read_file_content():
try:
with open(file_name, "r") as file:
content = file.read()
print("--- File Content ---")
print(content)
except FileNotFoundError:
print(f"Error: {file_name} does not exist yet.")
# Main execution block
if __name__ == "__main__":
write_to_file("Day 7: Functions, Scope, and Errors.")
append_to_file("Learning about File I/O today.")
read_file_content()