Categories

Dec 14, 2019 in Python
Q: python files and input outpout

1 Answer

Dec 14, 2019

Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again.

However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling.

In this section of the tutorial, we will learn all about file handling in python including, creating a file, opening a file, closing a file, writing and appending the file, etc.

Opening a file

Python provides the open() function which accepts two arguments, file name and access mode in which the file is accessed. The function returns a file object which can be used to perform various operations like reading, writing, etc.

The syntax to use the open() function is given below.

file object = open(<file-name>, <access-mode>, <buffering>)     

The files can be accessed using various modes like read, write, or append. The following are the details about the access mode to open a file.

SN Access mode Description

1 r It opens the file to read-only. The file pointer exists at the beginning. The file is by default open in this mode if no access mode is passed.

2 rb It opens the file to read only in binary format. The file pointer exists at the beginning of the file.

3 r+ It opens the file to read and write both. The file pointer exists at the beginning of the file.

4 rb+ It opens the file to read and write both in binary format. The file pointer exists at the beginning of the file.

5 w It opens the file to write only. It overwrites the file if previously exists or creates a new one if no file exists with the same name. The file pointer exists at the beginning of the file.

6 wb It opens the file to write only in binary format. It overwrites the file if it exists previously or creates a new one if no file exists with the same name. The file pointer exists at the beginning of the file.

7 w+ It opens the file to write and read both. It is different from r+ in the sense that it overwrites the previous file if one exists whereas r+ doesn't overwrite the previously written file. It creates a new file if no file exists. The file pointer exists at the beginning of the file.

8 wb+ It opens the file to write and read both in binary format. The file pointer exists at the beginning of the file.

9 a It opens the file in the append mode. The file pointer exists at the end of the previously written file if exists any. It creates a new file if no file exists with the same name.

10 ab It opens the file in the append mode in binary format. The pointer exists at the end of the previously written file. It creates a new file in binary format if no file exists with the same name.

11 a+ It opens a file to append and read both. The file pointer remains at the end of the file if a file exists. It creates a new file if no file exists with the same name.

12 ab+ It opens a file to append and read both in binary format. The file pointer remains at the end of the file.

Click here to read more about Python
Click here to read more about Insurance

Related questions

Madanswer
Nov 24 in Python
Nov 15 in Python
Aug 30 in Python
...