python3

python3 で、フォルダがなければ作成するプログラム

投稿日:

os.mkdir() 関数を使う。

すでにフォルダが存在している場合は、エラーを表示する。

import os 
folder_name = './python_create_folder'
    
try:
    os.mkdir(folder_name) 
    print('フォルダを作成しました')
except OSError as e: 
    print('エラー: ' + str(e))

結果

フォルダを作成しました

結果(すでに「python_create_folder」フォルダが存在する場合)

エラー: [Errno 17] File exists: './python_create_folder'

-python3
-,

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python でリストに、コロン演算子2つ(::)を使う

リストのコロンで指定できるパラメータには、「start, end, step」という意味がある。 以下の例では、:: 5 とした場合には、5 をステップとして、次に進んでいく。 ::-5 とした場合に …

no image

python3 で、pathlib を使ってファイルの拡張子を取得する方法

pathlib モジュールの stem を使う。 この書き方は、python 3.4 以降で使えるようになった。 https://docs.python.org/3/library/pathlib.h …

no image

matplotlib で、線の種類を変更する方法

「linestyle = 」として、線の種類を指定することができる設定の詳細は、次のリンクを参照。 https://matplotlib.org/stable/gallery/lines_bars_a …

no image

matplotlib で、グラフの線の幅を変更する方法

lw で、線幅を数字で指定すればよい。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [39, …

no image

matplotlib で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …