Python3-os-unlink

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

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

説明

  • unlink()メソッドは、ファイルパスを削除(削除)します。パスがディレクトリの場合、link:/python3/standard_exceptions [OSError]が発生します。 関数は remove()*と同じです。リンク解除名は、従来のUnix名です。

構文

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

os.unlink(path)

パラメーター

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

戻り値

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

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

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

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

# 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',
   'Java Multiple Inheritance', 'Java Multiple Inheritance_files',
   'java.ppt', 'python2'
]

The dir after removal of path : [
   'Applicationdocs.docx', 'book.zip',
   'Java Multiple Inheritance',
   'Java Multiple Inheritance_files', 'java.ppt', 'python2'
]