Python os.renames() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

The os.renames() method is used to recursively rename directories or files. Similar to rename().

Syntax

renames()The method syntax is as follows:

os.renames(old, new)

Parameters

  • old-- The directory to be renamed

  • new-- The new name for the file or directory. It can even be a file contained in a directory, or a complete 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
print "Current directory is: %s" %os.getcwd()

# List the directory
print "Directory is: %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 is: %s" %os.listdir(os.getcwd())

The output of the above program is:

当前目录为: /tmp
目录为:
 [  'a1.txt','resume.doc','a3.py','aa1.txt','Administrator','amrood.admin' ]
重命名成功。
目录为:
 [  'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions