You are here : python_2ososrename

os.rename() - os

             

The method rename() renames the file or directory src to dst.If dst is a file or directory(already present), OSError will be raised.


  • src -- This is the actual name of the file or directory.

  • dst -- This is the new name of the file or directory.


Syntax


os.rename(src, dst)


Example


# !/usr/bin/python

import os, sys

# listing directories
print "The dir is: %s"%os.listdir(os.getcwd())

# renaming directory ''tutorialsdir"
os.rename("tutorialsdir","tutorialsdirectory")

print "Successfully renamed."

# listing directories after renaming "tutorialsdir"
print "the dir is: %s" %os.listdir(os.getcwd())\


Output / Return Value

When we run above program, it produces following result:


The dir is: [ 'a1.txt','resume.doc','a3.py','tutorialsdir','amrood.admin' ] Successfully renamed. The dir is: [ 'a1.txt','resume.doc','a3.py','tutorialsdirectory','amrood.admin' ]


Limitations


Alternatives / See Also


Reference