Python3-os-rename
提供:Dev Guides
Python 3-os.rename()メソッド
説明
- rename()メソッドは、ファイルまたはディレクトリ *src を dst に名前変更します。 dst がファイルまたはディレクトリ(すでに存在する)の場合、link:/python3/standard_exceptions [OSError]が発生します。
構文
以下は* rename()*メソッドの構文です-
os.rename(src, dst)
パラメーター
- src -これはファイルまたはディレクトリの実際の名前です。
- dst -これは、ファイルまたはディレクトリの新しい名前です。
戻り値
このメソッドは値を返しません。
例
次の例は、rename()メソッドの使用法を示しています。
# !/usr/bin/python3
import os, sys
os.chdir("d:\\tmp")
# listing directories
print ("The dir is: %s"%os.listdir(os.getcwd()))
# renaming directory ''tutorialsdir"
os.rename("python3","python2")
print ("Successfully renamed.")
# listing directories after renaming "python3"
print ("the dir is: %s" %os.listdir(os.getcwd()))
結果
上記のプログラムを実行すると、次の結果が生成されます-
The dir is: [
'Applicationdocs.docx', 'book.zip', 'foo.txt',
'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
'java.ppt', 'Python3'
]
Successfully renamed.
the dir is: [
'Applicationdocs.docx', 'book.zip', 'foo.txt',
'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
'java.ppt', 'python2'
]