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

python3 で、csv ファイルを読み込んで条件をみたす行の内容を表示する方法

csv.reader で読み込み、各行を読み込んで判定する。 例 import csv with open(‘data.csv’, ‘r’) as csv_file: reader = csv.rea …

no image

python3 でランダムな整数の配列を作る方法

random モジュールを使う。randint() の引数で、配列の最小値と最大値を指定できる。 例 import random random_arr = [random.randint(-2 …

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …