Easy Tutorial
❮ Python Func Ascii Ref Math Isnan ❯

Python3 os.rename() Method

Python3 OS File/Directory Methods


Overview

The os.rename() method is used to rename a file or directory from src to dst. If dst is an existing directory, an OSError will be raised.

Syntax

The syntax for the rename() method is as follows:

os.rename(src, dst)

Parameters

-

src -- The name of the directory to be modified

-

dst -- The new name of the directory

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the rename() method:

#!/usr/bin/python3

import os, sys

# List directory
print ("Directory is: %s"%os.listdir(os.getcwd()))

# Rename
os.rename("test","test2")

print ("Renaming successful.")

# List directory after renaming
print ("Directory is: %s" %os.listdir(os.getcwd()))

Executing the above program will output:

Directory is:
[  'a1.txt','resume.doc','a3.py','test' ]
Renaming successful.
[  'a1.txt','resume.doc','a3.py','test2' ]

Python3 OS File/Directory Methods

❮ Python Func Ascii Ref Math Isnan ❯