參考:https://www.cnblogs.com/lovychen/p/5291673.html
-----------------------------------------------------------------------------------------------------
#統計 /home/dir/ 下的文件夾個數
import os
path ="home/dir"
count = 0
for fn in os.listdir(path): #fn 表示的是文件名
? ? ? ? count = count+1
print count
-----------------------------------------------------------------------------------------------------
獲取文件夾下的文件的個數:
import os
path = os.getcwd()? ? #獲取當前路徑
count = 0
for root,dirs,files in os.walk(path):? ? #遍歷統計
? ? ? for each in files:
? ? ? ? ? ? count += 1? #統計文件夾下文件個數
print count? ? ? ? ? ? ? #輸出結果
-----------------------------------------------------------------------------------------------------