File Handling in Python and it’s Example


Introduction To File Handling in Python

The concept of file handling has over numbers of other languages, but the implementation is either complicated or lengthy, but file handling in Python, this concept here is also easy and short. Each line of a file is terminated with a special character, called the EOL(End of Line characters). It ends the current line.

Handling Python file

If you are working on software or any industry has large data, then we cannot expect those data to be store in a variable and that variable are volatile in nature.

If the file is non-volatile in nature, the data will be store permanently.

Data Science With Python

Types of File: There are two types of files in python.

  1. Binary file
  2. Text file

1. Binary File: Most of the file is in Binary mode. All binary files follow some specific format. Binary file required special software to read a file.

For Example:

  • Document files: .pdf, .doc, .xls etc.
  • Database files: .mdb, .sqlite etc
  • Image files: .png, .jpg, .gif, etc.
  • Video files: .mp4, .3gp, .avi etc.
  • Audio files: .mp3, .wav, .mp4, etc.
  • Archive files: .zip, .rar, etc.

2. Text File: Text file means, don’t have any specific encoding and it can be opened in normal text.

For Example:

  • Web standards: html, XML, CSS, JSON etc.
  • Source code: c, js, py, java etc.
  • Documents: txt, tex,etc.
  • Tabular data: csv, tsv etc.
  • Configuration: ini, cfg etc.

For open() function, you can refer to the blog based on python file reading.



For file reading,

X  = open.(“sample.txt”)

Another method,

X  = open.(“sample.txt”, ‘r’)

‘r’ used for reading a file.


In this article, you can learn types of files and how to read it in different modes.


Please enter your comment!
Please enter your name here