Python3-os-remove

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

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

説明

メソッド* remove()*は、ファイルパスを削除します。 パスがディレクトリの場合、link:/python3/standard_exceptions [OSError]が発生します。

構文

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

os.remove(path)

パラメーター

*path* -これは削除されるパスです。

戻り値

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

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

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

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

# removing
os.remove("test.java")

# listing directories after removing path
print ("The dir after removal of path : %s" %os.listdir(os.getcwd()))

結果

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

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