[解決!Python]os.pathモジュールのexists関数を使ってパスが存在するかどうかを確認するには:解決!Python exists関数に渡したパスが実際に存在するかどうかを確認したり、isdir/isfile関数と組み合わせて処理を振り分けたりする方法を紹介する。 import os from os.path import exists, splitext, isdir, isfile mydir = 'mydir' myfile = 'myfile.txt' nofile_or_dir = 'nofile_or_dir' # mydirディレクトリとmyfile.txtファイルを作成 os.mkdir(mydir) with open(myfile, 'w'): pass print(exists(mydir)) # True print(exists(myfile))
![[解決!Python]os.pathモジュールのexists関数を使ってパスが存在するかどうかを確認するには](https://cdn-ak-scissors.b.st-hatena.com/image/square/6e61ff5dd64d5ad1c2f4ec0d433f563ecc9b9e05/height=288;version=1;width=512/https%3A%2F%2Fimage.itmedia.co.jp%2Fait%2Farticles%2F2210%2F25%2Fcover_news035.png)