Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.renames() method

Python os.renames() method

Python File methods 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

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

print

"Current directory: %s"

%

os

.

getcwd

(

)

List the directory

print

"Directory: %s"

%

os

.

listdir

(

os

.

getcwd

(

)

)

Rename "aa1.txt"

os

.

renames

(

"aa1.txt"

,

"newdir/aanew.txt"

)

print

"Renamed successfully."

List the renamed file "aa1.txt"

print

"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' ]

Python File methods Python OS file/directory methods