我想将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