Python3-os-removedirs

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

Python 3-os.removedirs()メソッド

説明

メソッド* removedirs()*は、dirsを再帰的に削除します。 リーフディレクトリが正常に削除されると、removeirsはpathに表示されているすべての親ディレクトリを連続して削除しようとします。 リーフディレクトリを正常に削除できなかった場合は、OSErrorが発生します。

構文

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

os.removedirs(path)

パラメーター

*path* -これはディレクトリのパスであり、削除する必要があります。

戻り値

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

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

# !/usr/bin/python3
import os, sys

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

# removing
os.removedirs("home\\monthly\\daily")

# listing directories after removing directory
print ("The dir after removal is:" %os.listdir(os.getcwd()))

結果

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

The dir is: [
   'Applicationdocs.docx', 'book.zip', 'foo.txt', 'home',
   'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
   'java.ppt', 'ParallelPortViewer'
]
The dir after removal is: [
   'Applicationdocs.docx', 'book.zip', 'foo.txt',
   'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
   'java.ppt', 'ParallelPortViewer'
]