描述
方法renames()是遞歸目錄或文件重命名函數。它的函數與 os.rename() 相同,但它還將文件移動到不存在的目錄或整個目錄樹。
用法
以下是語法renames()方法 -
os.renames(old, new)
參數
old- 這是要重命名的文件或目錄的實際名稱。
new- 這是文件或目錄的新名稱。它甚至可以將文件包含到一個目錄中,或整個目錄樹中,這些目錄不存在。
返回值
此方法不返回任何值。
示例
下麵的例子展示了 renames() 方法的用法。
# !/usr/bin/python3
import os, sys
os.chdir("d:\\tmp")
print ("Current directory is:%s" %os.getcwd())
# listing directories
print ("The dir is:%s"%os.listdir(os.getcwd()))
# renaming file "aa1.txt"
os.renames("foo.txt","newdir/foonew.txt")
print ("Successfully renamed.")
# listing directories after renaming and moving "foo.txt"
print ("The dir is:%s" %os.listdir(os.getcwd()))
os.chdir("newdir")
print ("The dir is:%s" %os.listdir(os.getcwd()))
結果
當我們運行上述程序時,它會產生以下結果 -
Current directory is:d:\tmp The dir is:[ 'Applicationdocs.docx', 'book.zip', 'foo.txt', 'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 'java.ppt', 'python2' ] Successfully renamed. The dir is:[ 'Applicationdocs.docx', 'book.zip', 'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 'java.ppt', 'newdir', 'python2' ]
結果
文件foo.txt此處不可見,因為它已移至newdir並重命名為foonew.txt.目錄newdir其內容如下所示:
The dir is:['foonew.txt']
相關用法
- Python 3 os.rename()用法及代碼示例
- Python 3 os.removedirs()用法及代碼示例
- Python 3 os.remove()用法及代碼示例
- Python 3 os.readlink()用法及代碼示例
- Python 3 os.read()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.utime()用法及代碼示例
- Python 3 os.tcgetpgrp()用法及代碼示例
- Python 3 os.statvfs()用法及代碼示例
- Python 3 os.lchown()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.renames() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。