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