Python File Handling Tutorial
In general a file is a named location to store information. In python there are various file related operations that we can perform. In this context, first we have to open a file. Python has a built-in open() function to open a file. This function returns a file object, also called a handle. At the time of opening a file, we can specify a mode like read, write, append or create.
Get Pythontpoints’s stories in your inbox
Join Medium for free to get updates from this writer.
There are four different modes to open a file:
- “r” — Read — Opens a file for reading, error if the file does not exist
- “w” — Write — Opens a file for writing, creates the file if it does not exist
- “a” — Append — Opens a file for appending, creates the file if it does not exist
- “x” — Create — Creates the specified file, returns an error if the file exists
- “t” — Text — Default value. Text mode
- “b” — Binary — Binary mode (e.g. images)
#Open a file and perform read operation
f1 = open("e:\demo.txt", "r")
print(f1.read())



