Thuta Learning
IntermediateProgrammingbeginner

Python File Handling

Relax. We'll talk through this in plain words — no textbook voice.

In Python, you can read and write files. The open() function is used for this. Using the with statement is best, since it closes the file automatically for you.

"r": Read

"w": Write

"a": Append

python
with open("myfile.txt", "w") as f:
    f.write("Hello, this is a new file.\n")
    f.write("This is the second line.")

print("File 'myfile.txt' has been created.")
You should see
File 'myfile.txt' has been created.
Python File Handling | Thuta Learning