0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

プログラムの最初にフォルダを作成する

Posted at

現在のディレクトリ直下に指定した名前のディレクトリを作成する。

やり方

import os

dir_name = "./{NAME}/"
os.makedirs(dir_name, exist_ok=True)

環境

python3.〜

注意点

引数について

引数名 役割
name ファイル名(第一引数)
mode
exist_ok 既に存在するけどok?
head
tail
cdir

引数名の確認は以下より
https://qiita.com/chankane/items/3909e9f2d1c5910cc60b

入力
import os

arg_names = os.makedirs.__code__.co_varnames
print(arg_names)
出力
('name', 'mode', 'exist_ok', 'head', 'tail', 'cdir')

引数のexist_okについて

True か Falseを受け取る
Trueだとすでに同じ名前のディレクトリがあってもエラーにならない
FalseはFileExistsErrorになる

os.mkdir() と os.makedirs() の違い

  • mkdir()は二階層下にディレクトリを作るときに一階層下のパスも完全一致でないとダメ
  • makedirs()は二階層下にディレクトリを作るとき一階層下に指定されたパスのディレクトリが無ければ作ってくれる

参考以下

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?