フォルダの作成
cv2.imwriteなど、フォルダが存在しないファイルを作成してくれない。
以下により、フォルダが無い場合だけ作成する。
import os
os.makedirs( path, exist_ok=True)
パイソンファイルの場所
pyファイルと同じフォルダに計算データを保存したい時。実行したターミナルの場所とpyファイルの場所が異なる時があるので、_file_で指定した方が確実。
import os
print('getcwd: ', os.getcwd())
print('__file__: ', __file__)
print('basename: ', os.path.basename(__file__))
print('dirname: ', os.path.dirname(__file__))
getcwd: /home/user 実行したターミナルの現在位置
file: /code/src_notebook_26_4category/save_path_test.py
basename: save_path_test.py
dirname: /code/src_notebook_26_4category
ファイルのパス
image_fileの場所を知りたいとき
フルパス
os.path.split(image_file)[0]
直上のフォルダ名
os.path.basename(os.path.dirname(image_file))