0
0

More than 1 year has passed since last update.

Pythonで新規ファイル作成時にディレクトリが存在しなければ作成する方法

Posted at

よく使用する便利な書き方なので備忘録として。

os.makedirsexist_ok=Trueとすると、既に存在するディレクトリに対しては何もしない。
引数がディレクトリなので、pathが出力ファイルの場合は以下のように指定する。

path = 'piyo/hoge/hoge.txt'
os.makedirs(os.path.dirname(path), exist_ok=True)

参考

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