0
0

More than 1 year has passed since last update.

python3小技

Last updated at Posted at 2022-02-09

フォルダの作成

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))

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0