![](http://python-guide.work/wp-content/themes/cocoon-master/images/b-woman.png)
Pythonで
・指定したフォルダ配下の
・ファイル一覧を取得
できます!
コード
ここでは例として
・デスクトップにあるフォルダ「test」配下のファイル一覧を取得
します。
※デスクトップ上に下記コードを記載した「sample.py」を作成します。
![フォルダ「test」](https://python-guide.work/wp-content/uploads/2023/08/py_get_file_list_001.png)
![フォルダ「test」配下には6ファイルある](https://python-guide.work/wp-content/uploads/2023/08/py_get_file_list_002.png)
import os
# 対象フォルダ
folder_path = r'C:\Users\lunch\Desktop\test'
# ファイル一覧を取得
with os.scandir(folder_path) as entries:
for entry in entries:
if entry.is_file():
print(entry.name)
実行結果
指定したフォルダ配下のファイル一覧を取得できました。
![実行結果](https://python-guide.work/wp-content/uploads/2023/08/py_get_file_list_003.png)
参考
上記で使用した以下の詳細は、公式サイトをご確認ください。
●標準ライブラリ「os」モジュールの「scandir」