Python3-os-rmdir

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

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

説明

メソッド* rmdir()*は、ディレクトリパスを削除します。 ディレクトリが空の場合にのみ機能します。それ以外の場合はlink:/python3/standard_exceptions [OSError]が発生します。

構文

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

os.rmdir(path)

パラメーター

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

戻り値

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

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

# !/usr/bin/python3
import os, sys
os.chdir("d:\\tmp")

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

# removing path
os.rmdir("newdir")

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

結果

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

the dir is: [
   'Applicationdocs.docx', 'book.zip',
   'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
   'java.ppt', 'newdir', 'python2'
]

Traceback (most recent call last):
   File "test.py", line 8, in <module>
   os.rmdir("newdir")
OSError: [WinError 145] The directory is not empty: 'newdir'

「newdir」ディレクトリが空ではないため、エラーが発生しています。 「newdir」が空のディレクトリである場合、これは次の結果を生成します-

the dir is: [
   'Applicationdocs.docx', 'book.zip',
   'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
   'java.ppt', 'newdir', 'python2'
]

the dir is: [
   'Applicationdocs.docx', 'book.zip',
   'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
   'java.ppt', 'python2'
]