Python os.path.exists関数を使ってフォルダとファイルの存在チェックサンプル

環境
Python 3.9.13
Windows 10 Home 64bit
PyCharm 2022.2.1 (Community Edition)

書式
os.path.exists('ファイル名’)
os.path.exists('ディレクトリ名’)
os.path.exists関数を使用してフォルダとファイルの存在チェックを行います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import os
print(os.path.exists('55.txt'))
# True
print(os.path.exists('11.csv'))
# True
print(os.path.exists('venv'))
# True
print(os.path.exists('11'))
# False
import os print(os.path.exists('55.txt')) # True print(os.path.exists('11.csv')) # True print(os.path.exists('venv')) # True print(os.path.exists('11')) # False
import os

print(os.path.exists('55.txt'))
# True

print(os.path.exists('11.csv'))
# True

print(os.path.exists('venv'))
# True

print(os.path.exists('11'))
# False

実行結果
True
True
True
False

Python

Posted by arkgame