Latest web development tutorials

Python os.renames () method

Python File (File) method Python OS file / directory methods


Outline

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

grammar

renames () method syntax is as follows:

os.renames(old, new)

parameter

  • old - to be renamed directory

  • new - the new name of the file or directory.It can even be in the directory containing the file or complete directory tree.

return value

This method does not return value

Examples

The following example demonstrates renames () method of use:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

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 above program output is:

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

Python File (File) method Python OS file / directory methods