LoginSignup
6
11

More than 3 years have passed since last update.

Python フォルダの作成 既存かチェックあり

Posted at

背景

Pythonでフォルダ作成のプログラミングコードはたくさんありますが、
念のため備忘録を作成します。

サンプルコード

フォルダを作成することはできますが、
そのフォルダが既存だとエラーが出ます。
ifを使って回避します。

make_folder.py
import os

new_path = "demo_folder"#フォルダ名
if not os.path.exists(new_path):#ディレクトリがなかったら
    os.mkdir(new_path)#作成したいフォルダ名を作成

結果

1回目に実行したら、フォルダが作成されます。
2回目に実行したら、すでにフォルダが作成されているため何も起こりません。

6
11
0

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
6
11