【Python】フォルダとファイルの存在確認|pathlib
作成日:2025-09-28
更新日:2025-09-28

フォルダの存在確認
from pathlib import Path
def folder_exists(folder_path: str) -> bool:
return Path(folder_path).is_dir()ファイルの存在確認
from pathlib import Path
def file_exists(full_path: str) -> bool:
return Path(full_path).exists()pathlib
Path.is_dir()- 指定パスがフォルダなら
True - ファイルの場合は
False
- 指定パスがフォルダなら
Path.exists()ReturnTrueif the path points to an existing file or directory.
出典
- Python Docs. (n.d.). pathlib.Path.exists
In Python, always prefer pathlib for file operations—it’s cleaner and more modern.
Pythonでは、pathlib を使うのがきれい.
2025-09-28
編集後記:
この記事の内容がベストではないかもしれません。
記事一覧
-

[Python]endswith+ lower 【Python】拡張子の存在確認|endswith + lower(Method Chaining) -

[Python]Convert a stringinto datetime 【Python】文字列を日付型に変える|datetime.strptime -

[Python]pathlibfor extension 【Python】文字列から拡張子を取得して、文字列で返す|pathlib.Path.suffix -

[Python]Search or Scanfor Excel book 【Python】ファイル名からExcelブックを取得する|openpyxl or win32com -

[Python]Excel datapandas.DataFrame 【Python】シートにあるデータを配列に格納する|pandas.DataFrame -

[Python]Stringsplit 【Python】文字列を抜き出す|.split