File Handling in Python

  • date 17th July, 2019 |
  • by Prwatech |

File Handling in Python Tutorial

File Handling in Python Tutorial, Welcome to the world of Python File Handling  Tutorial. In this tutorial, we will learn what is file handling in python and how Python handles files. Here, You will also learn Python File Handling Operations, how to open a file in Python which is helpful for any Python developers. Are you looking for the information of python file handling tutorial or Or the one who is very keen to explore the File Handling in Python with Examples that are available or Are you dreaming to become to certified Pro Python Developer, then stop just dreaming, get your Python certification course from India’s Leading Python training institute. Python File Handling operations is one of the essential concepts for all programmers, which is used either to write a file or read data from the file. Mostly, all the values or data are stored in Variables at run time and when program execution is completed, the data will get lost. Hence, all Programmers will save the data permanently using files. Do you want to know about how Python handles files and Python File Handling Operations, then just follow the below-mentioned File Handling in Python Tutorial for Beginners from Prwatech and take advanced Python training like a Pro from today itself under 10+ years of hands-on experienced Professionals.

What is File Handling in Python?

In programming whatever operations we perform over variables and functions are temporary in nature. We cannot store it permanently use it. So, in order to have permanent storage of code we store it as a file. For handling these files we use File Handling techniques in Python. Python, like other languages, allows users to handle files i.e., to read and write files with many other file handling options.

How Python Handle Files?

Python file handling is essential for programmers as it has to deal with different operations like reading data from file or writing or adding data to file. Improving a single file operation can help you produce a high-performing application or a robust solution for automated software testing. Processing of file is done in the following format: The file is opened returning filehandle. The file used to perform different actions like reading data, writing data, etc. The file is closed.

Python File Handling Operations

Use of open():

file object = open(file_name [, access_mode][, buffering]) The parameters in syntax have the following significance: <access_mode> - This is an integer field that represents the opening mode of file e.g., read, write, rename, delete, append, etc. It’s an elective parameter. It is, by default, set to read-only “r” mode. So we get data in text format due to reading mode. Different modes are explained below with their notations and syntax. It’s also desirable for accessing the non-text files like an image or the Exe files. <buffering>- For this field default value is 0.It means buffering will not take place. The line buffering will take place if the value is 1 while accessing the file. If it’s greater than 1, then the buffering takes place according to the buffer size. If the value is less than 1 i.e. negative, then the default behavior is taken into account. <file_name>- This field is a string indicating the name of the file expected to be accessed. There are different modes while opening a file.

Modes for opening a file

Let’s see the different modes of operation for a file.

Read mode

Notation: ‘r’ This opens the file in reading mode. It’s syntax is: file = open(“testfile_name.txt”, “r”)

Append Mode

This opens a  file for appending mean adding any data in it. It’s syntax is: file= open(“testfile_name.txt”, “a”)

Write Mode

This opens the file in write mode. It’s syntax is: file= open(“testfile_name.txt”, ‘w”)

Create Mode

This opens file in create mode means it creates a file with a specific name It’s syntax is: file= open(“testfile_name.txt”, “x”)  

Text Mode

This opens the file in text mode. This means it creates a text file. It’s syntax is: file= open(“testfile_name.txt”, ‘rt”)

Binary Mode

This opens the file in binary mode. This means it creates a binary file of the mentioned file. It’s syntax is: file= open(“testfile_name.txt”, “rb”)

Read and Write Mode

This will allow read and write operations. It’s syntax is: file= open(“testfile_name.txt”, “r+”)

Read and write in binary mode

This will allow read and write operations in binary format. It’s syntax is: file= open(“testfile_name.txt”, “rb+”) In short, we can remind the tabular form as follows:
Name Indication Description Syntax
Read “r” Opens a file in reading mode f=open(“testfile_name.txt”, “r”)
Append “a” Opens a file for appending f=open(“testfile_name.txt”, “a”)
Write “w” Opens a file in write mode f=open(“testfile_name.txt”, “w”)
Create “x” Creates a file with a specified name f=open(“testfile_name.txt”, “x”)
Text “t” Creates a text file f=open(“testfile_name.txt”, “rt”)
Binary “b” Creates a binary file f=open(“testfile_name.txt”, “rb”)
Read andWrite “r+” Allows file for reading and writing only f=open(“testfile_name.txt”, “r+”)
Read and write in binary “rb+” Allows reading and writing files in binary format f=open(“testfile_name.txt”, “rb+”)

Use of reading ()

read(): It is used to read the file which is already open. Syntax: Ex) f = open(“a.txt”, “r”) print( We can also extract first ‘n’ number of characters from file. print( It will give first 4 characters in file.

Use of readlines ()

This command is used to read one line. Syntax: Ex) f = open(“a.txt”, “r”) print(f.readline())

Use of close()

It is simply used to a closed line Syntax: filename.close() Ex) f = open (“a.txt”, “r”) print( f.close()

Use of write() mode for file creation

It is command which allows to write in particular file. f=open('test.txt','w') f.write("Writing in this file") f.close()

Use of append() mode

It will append or add the contents in existing file. f=open('test.txt','a') f.write("Adding this line to file") f.close()

Renaming File

We can change file name with rename() function as follows: importos os.rename(file_current_name, file_new_name)It will rename the file with new assigned name. Example: import os os.rename(“test_file.txt”, “python_file.txt”) We hope you understand File Handling in Python Tutorial concepts. Get success in your career as a Python developer by being a part of the Prwatech, India's leading Python training institute in Bangalore.

Quick Support

image image