python 中os模塊os.path.exists()的用法說明
os即operating system(操作系統(tǒng)),Python 的 os 模塊封裝了常見的文件和目錄操作。
os.path模塊主要用于文件的屬性獲取,exists是“存在”的意思,所以顧名思義,os.path.exists()就是判斷括號里的文件是否存在的意思,括號內(nèi)的可以是文件路徑。
舉個栗子:user.py為存在于當前目錄的一個文件
輸入代碼:import ospath = os.path.exists(’user.py’)print(path)輸出結果:
True Process finished with exit code 0
如果不存在,返回的則是FALSE。
補充:
Python中os.path和os.makedirs的運用判斷文件或文件夾是否存在,創(chuàng)建文件夾
import osimport numpy as np data = np.array([1, 2, 3])if not os.path.exists('./data/'): print('# path not exists') os.makedirs('./data/') if not os.path.exists('./data/data.npy'): print('# data.npy not exists') np.save('./data/data.npy', data) print('# path exists? :', os.path.exists('./data/'))print('# data.npy exists? :', os.path.exists('./data/data.npy'))運行結果:
# path not exists# data.npy not exists# path exists? : True# data.npy exists? : True
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章:
1. ajax請求添加自定義header參數(shù)代碼2. ASP基礎知識VBScript基本元素講解3. Kotlin + Flow 實現(xiàn)Android 應用初始化任務啟動庫4. Python requests庫參數(shù)提交的注意事項總結5. Gitlab CI-CD自動化部署SpringBoot項目的方法步驟6. python操作mysql、excel、pdf的示例7. vue-electron中修改表格內(nèi)容并修改樣式8. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式9. 利用CSS3新特性創(chuàng)建透明邊框三角10. axios和ajax的區(qū)別點總結
