Python os.renames() method
Python OS file/directory methods
Overview
The os.renames() method recursively renames directories or files. It is similar to rename().
Syntax
The **renames()** method syntax is as follows:
os.renames(old, new)
Parameters
-
old -- The directory to rename
-
new -- The new name of the file or directory. It can even be a file contained in the directory, or a whole directory tree.
Return Value
This method has no return value
Example
The following example demonstrates the use of the renames() method:
Example
#!/usr/bin/python
-- coding: UTF-8 --
import
os
,
sys
"Current directory: %s"
%
os
.
getcwd
(
)
List the directory
"Directory: %s"
%
os
.
listdir
(
os
.
getcwd
(
)
)
Rename "aa1.txt"
os
.
renames
(
"aa1.txt"
,
"newdir/aanew.txt"
)
"Renamed successfully."
List the renamed file "aa1.txt"
"Directory: %s"
%
os
.
listdir
(
os
.
getcwd
(
)
)
The output of the above program is:
当前目录为: /tmp
Directory is:
[ 'a1.txt','resume.doc','a3.py','aa1.txt','Administrator','amrood.admin' ]
重命名成功。
Directory is:
[ 'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]