「Python」フォルダの中のフォルダ、ファイルを1件ずつ取得する

環境
PyCharm 2021.3
Python 3.9.7

書式
リスト =os.listdir('フォルダのパス’)
listdir関数を使ってフォルダのフォルダ、ファイルをリストに取得します。
リストから「for~in」を使ってフォルダのフォルダ、ファイルを1件ずつ取得します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import os
print("フォルダの中のファイルを取得する結果")
for fme in os.listdir('C:\\study\\skill\\python'):
print(fme)
import os print("フォルダの中のファイルを取得する結果") for fme in os.listdir('C:\\study\\skill\\python'): print(fme)
import os

print("フォルダの中のファイルを取得する結果")
for fme in os.listdir('C:\\study\\skill\\python'):
    print(fme)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
フォルダの中のファイルを取得する結果
dirtest
test.py
test001.xlsx
test002.xlsx
フォルダの中のファイルを取得する結果 dirtest test.py test001.xlsx test002.xlsx
フォルダの中のファイルを取得する結果
dirtest
test.py
test001.xlsx
test002.xlsx

 

Python

Posted by arkgame