Python os.renames() 方法

Python 教程 · 2019-02-04 10:44:28

概述

os.renames() 方法用于递归重命名目录或文件。类似rename()。

语法

renames()方法语法格式如下:

os.renames(old, new)

参数

  • old -- 要重命名的目录

  • new --文件或目录的新名字。甚至可以是包含在目录中的文件,或者完整的目录树。

返回值

该方法没有返回值

实例

以下实例演示了 renames() 方法的使用:

#!/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())

执行以上程序输出结果为:

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

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

On LISP

On LISP

Paul Graham / Prentice Hall / 09 September, 1993 / $52.00

On Lisp is a comprehensive study of advanced Lisp techniques, with bottom-up programming as the unifying theme. It gives the first complete description of macros and macro applications. The book also ......一起来看看 《On LISP》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具