Types of file modes and attributes of file object in Python

 

File Modes and File Attributes

 

Types of modes of file object

 

We can open a file using open() and by providing different modes to it.
To close that file we just need to use close() method.

r– This is the default mode of file object.In this mode pointer is always placed at the start of the file.This is read only mode for text file.

example:

obj=open('itvoyagers.txt','r')

example:

obj=open('itvoyagers.txt') #by default opens file in read mode

r+– This mode is used to read as well as write in text file.In this mode pointer is always placed at the start of the file.

example:

obj=open('itvoyagers.txt','r+')

rb– Here b indicates binary file and rb indicates read only mode in binary format.This is the default mode of binary files. In this mode pointer is always placed at the start of the file.

example:

obj=open('itvoyagers.txt','rb')

rb+ – This mode is used to read as well as write in binary file .In this mode pointer is always placed at the start of the file.

example:

obj=open('itvoyagers.txt','rb+')

w – The file is opened in write only mode.If the file already exists then this mode overwrites the existing contents ,if file does not exists then new file is created for writing.

example:

obj=open('itvoyagers.txt','w')


w+
– The file is opened in write and read mode.If the file already exists then this mode overwrites the existing contents ,if file does not exists then new file is created for writing.

example:

obj=open('itvoyagers.txt','w+')

 

wb – The file is opened in write only mode in binary format.If the file already exists then this mode overwrites the existing contents ,if file does not exists then new file is created for writing.

example:

obj=open('itvoyagers.txt','wb')

wb+ – The file is opened in write and read mode in binary format.If the file already exists then this mode overwrites the existing contents ,if file does not exists then new file is created for writing.

example:

obj=open('itvoyagers.txt','wb+')

 

a– The file is opened in append mode for text files.If the file already exists then this mode appends contents to the existing contents without overwriting ,if file does not exists then new file is created for writing. The file pointer is placed at the end of the file if file already exists.

example:

obj=open('itvoyagers.txt','a') 

a+ – The file is opened in append and read mode for text files.If the file already exists then this mode appends contents to the existing contents without overwriting ,if file does not exists then new file is created for writing. The file pointer is placed at the end of the file if file already exists.

example:

obj=open('itvoyagers.txt','a+')

ab  – The file is opened in append mode for binary files.If the file already exists then this mode appends contents to the existing contents without overwriting ,if file does not exists then new file is created for writing. The file pointer is placed at the end of the file if file already exists.

example:

obj=open('itvoyagers.txt','ab')

ab+ – The file is opened in append and read mode for binary files.If the file already exists then this mode appends contents to the existing contents without overwriting ,if file does not exists then new file is created for writing. The file pointer is placed at the end of the file if file already exists.

example:

obj=open('itvoyagers.txt','ab+')

(Note : In read and write mode file pointer is always at the beginning of the file and in append mode it is in the end of the file if the file is already present else it creates a new file)

Types of attributes of file object

 

    1.file.closed : This will return True if file is closed,else returns False
    2.file.mode : This will return access mode of file in which the file has been opened
    3.file.name : This will return Name of the file
    4.file.softspace : This will return True if space explicitly required with the print ,else returns False.(This is available in 2.7.13 version)
"""
Author : ITVoyagers (https://itvoyagers.in/)


Date : 27th November 2018

Description : Program to use attributes of file object
"""
#create file object and open file in any suitable mode like 'wb'
obj=open('itvoyagers.txt','wb')
print("name of a file",obj.name)
print("mode of a file",obj.mode)
print("file open or close",obj.closed)
#obj.softspace is available in 2.7 version
print("softspace flag",obj.softspace)

(Note : To perform following code you first need to create a text file using notepad (itvoyagers.txt),
add some content in it and save on same location where python is installed to avoid clash in paths.
Then write below code in script and save with .py)

You can also check other file related operations and modes

Leave a Comment