Lesson 9 of 10
File Handling
Python can read and write files easily. Use open() with modes: r (read), w (write), a (append). The with statement automatically closes the file when done.
PYTHON
# Write to a file
with open("data.txt", "w") as file:
file.write("Hello, File!\n")
file.write("Second line")
# Read from a file
with open("data.txt", "r") as file:
content = file.read()
print(content)