Python-os-renames

提供:Dev Guides
移動先:案内検索

Python os.renames()メソッド

説明

Pythonメソッド* renames()*は、再帰的なディレクトリまたはファイルの名前変更機能です。 http://www.finddevguides.com/python/os_rename%20 [os.rename()]と同じ機能を果たしますが、存在しないディレクトリまたはディレクトリツリー全体にファイルを移動します。

構文

以下は* renames()*メソッドの構文です-

os.renames(old, new)

パラメーター

  • 古い-これは、名前を変更するファイルまたはディレクトリの実際の名前です。
  • new -これはファイルまたはディレクトリの新しい名前です。存在しないディレクトリまたはディレクトリのツリー全体にファイルを含めることもできます。

戻り値

このメソッドは値を返しません。

次の例は、renames()メソッドの使用方法を示しています。

# !/usr/bin/python

import os, sys
print "Current directory is: %s" %os.getcwd()

# listing directories
print "The dir is: %s"%os.listdir(os.getcwd())

# renaming file "aa1.txt"
os.renames("aa1.txt","newdir/aanew.txt")

print "Successfully renamed."

# listing directories after renaming and moving "aa1.txt"
print "The dir is: %s" %os.listdir(os.getcwd())

上記のプログラムを実行すると、次の結果が生成されます-

Current directory is:/tmp
The dir is:
 [  'a1.txt','resume.doc','a3.py','aa1.txt','Administrator','amrood.admin' ]
Successfully renamed.
The dir is:
 [  'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]

ファイル_aa1.txt_は、_newdir_に移動され、_aanew.txt_に名前が変更されたため、ここには表示されません。 ディレクトリ_newdir_とその内容は以下に示されています-

[ 'aanew.txt' ]