我想將Linux系統上每個子目錄中的文件數量彙總到Excel表格中。Python將每個子目錄中的文件數量輸出到csv文件
該目錄一般設置爲:maindir/person/task/somedata/files
。 但是,設置的子目錄有所不同(即,某些文件可能沒有'task
'目錄),所以我需要讓python遍歷文件路徑。
我的問題是我需要從'person
'的所有子目錄名稱,目前我的代碼(下面)只附加最近的目錄和文件數量。如果任何人都可以幫助我解決這個問題,將不勝感激!
import os, sys, csv
outwriter = csv.writer(open("Subject_Task_Count.csv", 'w'))
dir_count=[]
os.chdir('./../../')
rootDir = "./" # set the directory you want to start from
for root, dirs, files in os.walk(rootDir):
for d in dirs:
a = str(d)
count = 0
for f in files:
count+=1
y= (a,count)
dir_count.append(y)
for i in dir_count:
outwriter.writerow(i)
非常感謝你的幫助,這一個完美的工作,對不起,我沒有機會檢查,直到今天。 – user1843473