我想根据它们的扩展名对命令目录中的文件进行计数。 所以,我创建了一个包含cwd中所有文件的列表,然后是一个只包含扩展名的列表,然后我从该列表中创建了一个字典。我使用count参数创建了字典,但我不知道如何处理这个。我的字典看起来像“{'txt':0,'doc':0}”。python - 计数文件(无法计算字典中的值)
import os,glob
def myfunc(self):
mypath=os.getcwd()
filelist=glob.glob("*") #list with all the files in cwd
extension_list=[os.path.splitext(x)[1][1:] for x in filelist] #make list with the extensions only
print(extension_list)
count=0;
mydict=dict((x,count) for x in extension_list) #make dict with the extensions as keys and count as value
print(mydict)
for i in mydict.values(): #i must do sth else here..
count+=1
print(count)
print(mydict)
感谢这么简单! – George