找到檔案夾后如何獲取串列中的檔案夾數量?- 例如:[Folder1, Folder2, Folder3] 我通過以下方式打開此串列中包含的所有檔案夾:
for root, dirs, files in os.walk(path):
pass
在這個檔案夾中我有一些東西可以找到,例如:Folder2 中的檔案名 -> xxx.png 當我找到這個時,我想得到我串列中 Folder2 的數量,即 1([Folder1, Folder2, Folder3])
uj5u.com熱心網友回復:
使用這個例子:
import os
# folder path
dir_path = r'E:\account'
count = 0
# Iterate directory
for path in os.listdir(dir_path):
# check if current path is a file
if os.path.isfile(os.path.join(dir_path, path)):
count = 1
print('File count:', count)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/535023.html
標籤:Pythonfor循环
