當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python os.path.isfile()用法及代碼示例

Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。 os.path模塊是Python中OS模塊的子模塊,用於通用路徑名操作。

os.path.isfile()Python中的method方法用於檢查指定的路徑是否是現有的常規文件。

用法: os.path.isfile(path)

參數:
path:代表文件係統路徑的path-like對象。 path-like對象是表示路徑的字符串或字節對象。

返回類型:此方法返回布爾型的布爾值。如果指定的路徑是現有的常規文件,則此方法返回True,否則返回False。

代碼1:使用os.path.isfile()方法

# Python program to explain os.path.isfile() method  
    
# importing os module  
import os 
  
# Path 
path = '/home/User/Desktop/file.txt'
  
# Check whether the  
# specified path is  
# an existing file 
isFile = os.path.isfile(path) 
print(isFile) 
  
  
# Path 
path = '/home/User/Desktop/'
  
# Check whether the  
# specified path is  
# an existing file 
isFile = os.path.isfile(path) 
print(isFile)
輸出:
True
False

參考: https://docs.python.org/3/library/os.path.html



相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.path.isfile() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。