Lektion 9 af 10
Filhåndtering
Python kan læse og skrive filer med open(). Modes som r, w og a betyder read, write og append.
with-statement lukker filen automatisk bagefter.
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)