python 判斷文件是否存在和刪除文件的api (其中判斷文件在不在讓想起這個能夠強兼容jenkins工做目錄那個問題)

判斷文件在不在的api:html

os即operating system(操做系統),Python 的 os 模塊封裝了常見的文件和目錄操做。python

os.path模塊主要用於文件的屬性獲取,exists是「存在」的意思,因此顧名思義,os.path.exists()就是判斷括號裏的文件是否存在的意思,括號內的能夠是文件路徑。api

舉個栗子:spa

user.py爲存在於當前目錄的一個文件操作系統

輸入代碼: .net

import os
path = os.path.exists('user.py')
print(path)
輸出結果: code

True

Process finished with exit code 0
若是不存在,返回的則是FALSE。

原文連接:https://blog.csdn.net/u012424313/article/details/82216092htm

 

刪除文件的api:blog

if os.path.exists(dest_file):
  os.remove(dest_file)rem

參考: https://www.runoob.com/python/os-remove.html

相關文章
相關標籤/搜索