i

Python Programming

Renaming and Deleting Files in Python

Python os module provides methods that performs file-processing operations, such as renaming and deleting files.

The rename()-Method

The rename()-method takes 2 arguments current filename and new filename.

Syntax                        >>>os.rename(current_file_name, new_file_name)

Example

Below is example to rename an existing file test1.txt

The remove()-Method

This method remove() to delete files by supplying the name of the file to be deleted as the argument.

Syntax                        >>>os.remove(file_name)

Example

Below is the example to delete an existing file test2.txt: