I want to design a function as follows:
explore("jpg", "../test_media")
outputsample:
{'../test_media':1,'../test_media/travel_photos':3}
That is, the output of the function should be a dictionary where each key is the address of a folder and each value is the number of files with that extension directly in that folder. If a folder does not contain the file we want, it should not be in the dictionary.
The code I wrote myself:
import os
def explore(ttype, address):
list_dir = list(os.walk(address))
directoirs = dict()
for x in list_dir:
count = 0
if x[2]:
for y in x[2]:
t = y.split('.')
if t[1].lower() == ttype:
count += 1
if count:
directoirs[str(x[0])] = count
return directoirs
But it does not give the desired output