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

matplotlib で複数のグラフを並べて表示する方法

plot.subplots() でグラフの縦方向と横方向の数を指定する。 例 import numpy as np from matplotlib import pyplot as plt x1 = …

no image

matplotlib でグラフの中に注釈の文字を書く方法

annotate を使う。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace(-10,10,100) …

no image

matplotlib で、ヒストグラムを作成する方法

ヒストグラムを描くには、data_hist を使う。 ヒストグラムの縦棒の数は bins で指定する。 例 from matplotlib import pyplot as plt data_hist …

no image

python3 で csv ファイルを読み込んで、最初の数行を表示する方法

以下の例では、空の配列 data を用意しておき、最初の3行を読み込んでおく。 例 import csv with open(‘data.csv’,’r’) as csv_file: csv_read …

no image

python3 で、配列の最大値のインデックスを1つ求める方法

配列の index() メソッドを使うと、その値のインデックスを求めることができる。 最大値を求めるメソッド max と組み合わせて使う。 例 arr = [2,4,5,10,8,-3] in …