Python

Learn to handle files in Python

Learn to handle files in Python

When working with a large software application, there will be a large number of data to be processed. These cannot be stored in a variable as the variables are volatile in nature. In these situations, the data stored in files comes into play. Files are non – volatile in nature as these data can be permanently stored in a secondary device and these can be handled using python.

There are two types of files in python – Binary file and Text file. Most of the files we handle are actually binary files. For e.g., Document files (.pdf, .doc, .xls etc.), Image files (.png, .jpg,.gif etc.), video files (.mp4, .mkv etc.) ,Database files,Archive files, Executable files. We can open some files in the normal text editor but , we can read the content in them because all the binary files are encoded in binary format and thus, can only be understood by a machine. Opening such files require specific software to open it. For example, MS Word is required to open .doc files. Likewise, pdf reader is required to open .pdf files.

Text files on the other hand, don’t have any specific encoding and it can be opened in normal text editor itself. For e.g., Web standards(.html, .xml, .json etc.), source code(.py, .php, .js etc), Documents(.txt etc), Tabular data(.csv, .tsv etc).

There are 4 different operations that can be handled by python on files – Open, Read, Write, Close. Python has an built-in function called open( ) to open a file. Open method returns a file object which is used to access the write, read and other built-in methods.

file_object=open(file_name,mode)

file_name is the name of the file that has to be opened and it should have file extension. The mode in the syntax will tell what operation you want to do on a file.

  • r’ – Read Mode: Read mode is used only to read data from the file.
  • w’ – Write Mode: This mode is used when you want to write data into the file or modify it.Write mode overwrites the data present in the file.
  • a’ – Append Mode: Append mode is used to append data to the file.In, this mode, data will be appended at the end of the file pointer.
  • r+’ – Read or Write Mode: This mode is used when we want to write or read the data from the same file.
  • a+’ – Append or Read Mode: This mode is used when we want to read data from the file or append the data into the same file.

When we are using binary files, we have to use the same modes with the letter ‘b’ at the end, so that Python can understand that we are interacting with binary files.

  • wb’ – Open a file for write only mode in the binary format.
  • rb’ – Open a file for the read-only mode in the binary format.
  • ab’ – Open a file for appending only mode in the binary format.
  • rb+’ – Open a file for read and write only mode in the binary format.
  • ab+’ – Open a file for appending and read-only mode in the binary format.

File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files. Best python training in Kerala will provide the best training in all about file handling while creating the web applications. Choose a python full stack developer training in Kochi that will help you to reach the heights in your career.

Author: STEPS

Leave a Reply

Your email address will not be published. Required fields are marked *