Python3 Os Rename
# Python3.x 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 directory name to be modified
* **dst** -- The new directory name
### Return Value
This method does not return any value.
### Example
The following example demonstrates the use of the rename() method:
#!/usr/bin/python3import os, sys # List directoryprint ("Directory is: %s"%os.listdir(os.getcwd()))# Rename os.rename("test","test2")print ("Rename successful.")# List renamed directoryprint ("Directory is: %s" %os.listdir(os.getcwd()))
The output of executing the above program is:
Directory is:[ 'a1.txt','resume.doc','a3.py','test' ]Rename successful.[ 'a1.txt','resume.doc','a3.py','test2' ]
[ Python3 OS File/Directory Methods](#)
YouTip