Pythonでファイル、ディレクトリの有無をチェックするサンプルコードを以下に記します。
htmlinsert(): The given local file does not exist or is not readable.
$ python --version Python 2.7.3
$ lsb_release -d Description: Ubuntu 12.04.4 LTS
ファイルの有無を確認する構文は以下の通りです。
import os.path os.path.exists("ファイルorディレクトリ")
os.path.exists()で有無判定されTrueまたはFalseが返却されます。
以下にサンプルコードおよび実行出力結果を記します。
import os.path print os.path.exists("/etc") # directory print os.path.exists("/etc/passwd") # file print os.path.exists("/ETC") # directory print os.path.exists("/etc/hogehoge") # file
$ python exists.py True True False False
ファイルまたはディレクトリが存在すればTrueが返却し存在しない場合はFalseが返却されているのが確認できます。
以上、ファイル・ディレクトリの有無を確認するサンプルコードでした。