Remaining file methods in Python

Table of Contents

File methods

Summarizing file methods:

    open()
    close()
    read()
    write()
    rename()
    remove()

Among the above methods we have already explained open() ,read(),write() and close() in previous posts.
Now we will explain remove and rename methods of file object.

rename():

This method is used for renaming existing file.To use this method it is important to import os module.
Syntax :

obj.rename(old_filename , new_filename)

Example :

"""
Author : ITVoyagers (https://itvoyagers.in/)

Date :23rd December 2018

Description : Program to use of rename()
"""
#create file object and show the use rename()
import os
os.rename("myname.txt","yourname.txt")

remove():

This method is used for removing existing file.To use this method it is important to import os module.
Syntax :

obj.remove(filename)

Example :

"""
Author : ITVoyagers (https://itvoyagers.in/)

Date :23rd December 2018

Description : Program to use of remove()
"""

#create file object and show the use remove()
import os
os.remove("myname.txt")

You can also check other file related operations and modes

Leave a Comment