Python3 os.renames() Method

Python3 OS 文件/目录方法Python3 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 of 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:

#!/usr/bin/python3

import os, sys
print ("当前目录为: %s" %os.getcwd())

# 列出目录
print ("目录为: %s"%os.listdir(os.getcwd()))

# 重命名 "aa1.txt"
os.renames("aa1.txt","newdir/aanew.txt")

print ("重命名成功。")

# 列出重命名的文件 "aa1.txt"
print ("目录为: %s" %os.listdir(os.getcwd()))

The output of the above program execution is:

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

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions