0
0

More than 1 year has passed since last update.

pythonでディレクトリが存在しているかどうかをチェックする

Posted at

pythonでディレクトリが存在しているかどうかをチェックするにはos.path.exists()を使う、
ディレクトリを作成するにはos.makedirs()を使う

test.py
import os

checkdir = './testdir'
if os.path.exists(checkdir) == False:
    print("{} not exists".format(checkdir))
    print("{} create path".format(checkdir))
    os.makedirs(checkdir)
else:
    print("{} exists".format(checkdir))

実行する

$ python test.py 
./testdir not exists
./testdir create path
$ python test.py 
./testdir exists
$ ls testdir/

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0