這os.path.exists()
提供的方法os
標準庫模塊返回True
如果文件存在,並且False
如果不。
使用方法如下:
import os
filename = ‘/Users/flavio/test.txt’
exists = os.path.exists(filename)
print(exists) # True
更多python教程:
- Python簡介
- 在macOS上安裝Python 3
- 運行Python程序
- Python 2和Python 3
- 使用Python的基礎
- Python數據類型
- Python運算子
- Python字符串
- Python布爾值
- Python數字
- Python,接受輸入
- Python控制語句
- Python列表
- Python元組
- Python集
- Python字典
- Python函數
- Python對象
- Python循環
- Python模塊
- Python類
- Python標準庫
- 調試Python
- Python變量範圍
- Python,從命令行接受參數
- Python遞歸
- Python嵌套函數
- Python Lambda函數
- Python閉包
- Python虛擬環境
- 使用Python將GoPro用作遠程網絡攝像頭
- Python,如何從字符串創建列表
- Python裝飾器
- Python Docstrings
- Python自省
- Python註釋
- Python,如何列出目錄中的文件和文件夾
- Python,如何檢查數字是否為奇數或偶數
- Python,如何獲取文件的詳細信息
- Python,如何檢查文件或目錄是否存在
- Python異常
- Python,如何創建目錄
- Python,如何創建一個空文件
- Python,`with`語句
- Python,創建網絡請求
- Python,使用`pip`安裝第三方軟件包
- Python,讀取文件內容