Os Rename
# Python2.x Python os.rename() Method
[ Python 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 file or directory to be renamed.
* **dst** -- The new name for the file or directory.
### Return Value
This method does not return a value.
### Example
The following example demonstrates the use of the rename() method:
#!/usr/bin/python# -*- coding: UTF-8 -*-import 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' ]
[ Python OS File/Directory Methods](#)
YouTip