LoginSignup
1
3

More than 3 years have passed since last update.

python ディレクトリ作成 ディレクトリ存在する場合対応

Last updated at Posted at 2020-09-14

ディレクトリ作成時にディレクトリが存在する場合エラーになる。

FileExistsError: [WinError 183] 既に存在するファイルを作成することはできません。

存在チェックする方法

import os

DIR_NAME = 'dirname'
if not os.path.exists(DIR_NAME):
    os.mkdir(DIR_NAME)

例外処理する方法

import os

DIR_NAME = 'dirname'
try:
    os.mkdir(DIR_NAME)
except OSError as ex:
    print(ex)
1
3
2

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
1
3