i want to count files in the command directory based on their extension.
So, i created a list with all the files in the cwd ,then a list with only the extensions and then i made a dict from that list.I made the dict with a count parameter but i don’t know how to handle this.My dict looks like “{‘txt’:0,’doc’:0}”.
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)
Just iterate through the extension list and increment the dictionary value: