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

gnuplot で、グラフの形を正方形にする方法

set size square または、その省略形で set size sq というコマンドを使えば正方形の中にグラフを描くことができる。 例 set size square plot cos(x) …

no image

python3 で区切り文字を省略して文字列を分割する

string の split 関数は、区切り文字を指定しない場合には半角スペースや改行・タブ文字を使って自動的に分割してくれる。分割結果はリストで返される。 例 str1 = ‘Hello, nice …

no image

python3 で整数の割り算、商と余りを求める方法

floor divisionを使う。 例 a = 15 // 7 b = 15 % 7 print(a) print(b) 結果 2 1

no image

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

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

no image

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

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