Files
In Python, you can work with files using the built-in open() function. Below is an example of how to read from and write to a file.
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, World!\n")
file.write("This is a sample file.\n")
# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
If you want to append rather than overwrite, you can open the file in append mode:
# Appending to a file
with open("example.txt", "a") as file:
file.write("Appending a new line.\n")