Python Files IO

Python File Handling:

File handling simply means to open a file and to process it according to the required tasks. Python facilitates several functions to create, read, write, append, delete and close files.

❏     Open File:

To open a file in Python, open() function is used.

Syntax:

f = open(“filename“, “mode“)

Filename:  This parameter specifies the name of the file to be opened.

Mode: This parameter specifies the mode in which the file should be opened.

Different MODES of open function:

$mode MODE DESCRIPTION
r Read only mode Pointer starts from the beginning of the file.
w Write only mode Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
a Write only mode Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
r+ Read Write mode Pointer starts from the beginning of the file.
w+ Read Write mode Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
a+ Read Write mode Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
rb Read only mode in Binary format. Pointer starts from the beginning of the file.
wb Write only mode in Binary format. Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
ab Write only mode in Binary format. Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
rb+ Read Write mode in Binary format. Pointer starts from the beginning of the file.
wb+ Read Write mode in Binary format. Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
ab+ Read Write mode in Binary format. Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.

❏     Close File:

To close a file in Python, close() function is used.

Syntax:

f.close()

❏     Read File:

●      To read a file in Python, read() function is used.

Syntax:

f.read()

To read a line of a file in Python, readline() function is used.

Syntax:

f.readline()

❏     Write File:

To write a file in Python, write() function is used.

Syntax:

f.write(statement)

❏     Delete File:

●      To delete a file in Python, os is imported and then os.remove() function is used.

Syntax:

import os

os.remove(“filename“)

To delete a folder in Python, os is imported and then os.rmdir() function is used.

Syntax:

import os

os.rmdir(“filename“)

Python File Handling Methods:

METHODS SYNTAX USES
rename() os.rename(“existing_file_name”, “new_file_name”) To replace the existing python file name with a new python file name.
remove() os.remove(“file_name”) To delete a python file.
mkdir() os.mkdir(“file_name“) To create a directory to store python files.
chdir() os.chdir(“file_name“) To change the current working directory.
rmdir() os.rmdir(“directory_name”) To delete a directory.
tell() To get the exact position in the python file.
getcwd() os.getcwd() To get the current working directory.

Example:

f = open("pythonex.txt", "w")
f.write("HELLO PYTHON!\nPython facilitates several functions to create, read, write, append, delete and close files.")
f.close()
 
f = open("pythonex.txt", "r")
b = f.read()
print b
f.close()

Output: