描述
方法unlink()移除(刪除)文件路徑。如果路徑是目錄,則引發 OSError。函數與 remove() 相同; unlink 名稱是其傳統的 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.htm', 'Java Multiple Inheritance_files', 'java.ppt', 'python2' ] The dir after removal of path:[ 'Applicationdocs.docx', 'book.zip', 'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 'java.ppt', 'python2' ]
相關用法
- Python 3 os.utime()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.rename()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.renames()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.tcgetpgrp()用法及代碼示例
- Python 3 os.statvfs()用法及代碼示例
- Python 3 os.lchown()用法及代碼示例
- Python 3 os.mknod()用法及代碼示例
- Python 3 os.lstat()用法及代碼示例
- Python 3 os.tmpfile()用法及代碼示例
- Python 3 os.getcwd()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.unlink() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。