file processing modes supported by Python

Post Reply
swathi
Posts: 134
Joined: Fri Nov 23, 2018 2:52 am

file processing modes supported by Python

Post by swathi » Fri Nov 30, 2018 7:21 am

Explain the use of with statement Explain all the file processing modes supported by Python?

AyubHussain
Posts: 138
Joined: Fri Nov 23, 2018 2:48 am

Re: file processing modes supported by Python

Post by AyubHussain » Fri Nov 30, 2018 7:23 am

use of with statement :

In python generally “with” statement is used to open a file, process the data present in the file, and also to close the file without calling a close() method. “with” statement makes the exception handling simpler by providing cleanup activities.

General form of with:

with open(“filename”, “mode”) as file-var:
processing statements
note: no need to close the file by calling close() upon file-var.close()

file processing modes supported by Python :

Python allows you to open files in one of the three modes. They are:

read-only mode, write-only mode, read-write mode, and append mode by specifying the flags “r”, “w”, “rw”, “a” respectively.

A text file can be opened in any one of the above said modes by specifying the option “t” along with

“r”, “w”, “rw”, and “a”, so that the preceding modes become “rt”, “wt”, “rwt”, and “at”.A binary file can be opened in any one of the above said modes by specifying the option “b” along with “r”, “w”, “rw”, and “a” so that the preceding modes become “rb”, “wb”, “rwb”, “ab”.

Post Reply

Return to “Python Modules and Packages”