os.path.isfile()
函數用于檢查給定的路徑是否是一個存在的文件
import os
file_path = "path/to/your/file"
if os.path.isfile(file_path):
print("The given path is a file.")
else:
print("The given path is not a file or does not exist.")
如果你想要檢查文件的權限,可以使用 os.access()
函數。例如,要檢查文件是否可讀,可以這樣做:
import os
file_path = "path/to/your/file"
if os.access(file_path, os.R_OK):
print("The file is readable.")
else:
print("The file is not readable or does not exist.")