Introduction to File Handling in Python | Python Programming

python-video-tutorials

File Handing: File handling in Python is the way to store data in a file. These stored data can be used later for performing various operations.

file-handling
.Why we need File Handling: Till know we are writing programs in such a way that we are getting input from user and returning the output. We are not storing any value/output anywhere. But in real life applications, we make some applications which store the values and that values can be used later for various operation.

Example: We can take the example of login/registration system. When user do registration by filling their information. Their info. is stored in file or databases. And later when user logins by filling their login credentials, then it search for the details entered by user. If details exist in file or database then user successfully login.

How many types of Data Files in Python?

There are three types of files in python: 
1). Text files
2). Binary files
3). CSV files

(i). Text files: Text files are the files which store information in ASCII and Unicode characters. Each line of text in text file is terminated by EOL (End of Line) special character ie. full stop(.). We use .txt extension for saving text files. If we do not save our file by using .txt extension then by default extension is file.

(ii). Binary files: Binary files stores the information in binary format ie. 1 or 0. Our machine also uses binary languages to store information in our memory. There is no delimiter for a line. Translator are not require by machine to translate binary files as machine can understand binary format text.

Translators are required in case of text files as machine do not understand text format file. So translator translates text to binary format and hence file is stored into the machine memory. Binary files are more secure.

(iii). CSV files: CSV files are comma separated values file. We will discuss about this file later in our Data Analytics topic.

difference-between-text-and-binary-files

Text Files Binary Files
It stores information in ASCII and Unicode characters. It stores information in the form of images, audio etc.
Text files are slower as compared to binary files. Binary files are faster to read and write for programs.
It uses extension .txt. It uses .dat extension.
Special character full stop(.) is used to terminate line. There is no delimiter in Binary file.
Easy to understand by humans. Difficult to understand by humans.

 

file-access-modes-in-python

File Modes Modes Descriptions
r Opens file for reading and this is the Default Mode.
rb Opens file for reading only in Binary format. Default Mode for Binary files.
r+ Opens file for both reading and writing purposes.
rb+ Opens file for both reading and writing purposes in Binary format.
w Opens file for writing only. If file already exists, then it overwrites the file else new file will be created.
wb Opens file for writing only in Binary format.
w+ Opens file for both reading and writing.
wb+ Opens file for both reading and writing in Binary format.
a Opens file for appending, pointer at end of the file. If file does not exist, it creates a new file for writing.
ab Opens file for appending in Binary format.
a+ Opens file for both appending and reading.
ab+ Opens file for both appending and reading in Binary format.