Python method renames() is recursive directory or file renaming function. It does the same functioning as os.rename(), but it also moves a file to a directory, or a whole tree of directories, that does not exist.
Following is the syntax for rename() method:
os.renames(old, new)
This method does not return any value.
The following example shows the usage of renames() method.
# !/usr/bin/python import os, sys print "Current directory is: %s" %os.getcwd() # listing directories print "The dir is: %s"%os.listdir(os.getcwd()) # renaming file "aa1.txt" os.renames("aa1.txt","newdir/aanew.txt") print "Successfully renamed." # listing directories after renaming and moving "aa1.txt" print "The dir is: %s" %os.listdir(os.getcwd())
When we run the above program, it produces the following result:
Current directory is: /tmp The dir is: [ 'a1.txt','resume.doc','a3.py','aa1.txt','Administrator','amrood.admin' ] Successfully renamed. The dir is: [ 'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]
The file aa1.txt is not visible here, as it is been moved to newdir and renamed as aanew.txt. The directory newdir and its contents are shown below:
[ 'aanew.txt' ]
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.