the function must define the recursive function (or using your own recursive function) es68(dir, extensions), which must count how many files of certain types are in a directory or in one of its subdirectories, and which receives as arguments:
dir: the path to the directory to search extensions: extensions (the last letters of the name of the files we are looking for)
import os
import os.path
def es68(dir, estensioni):
c = 0
for name in os.listdir(dir):
if name.startswith('.'): continue
if estensioni in name:
c = 1
pathname = os.path.join(dir, name)
if os.path.isdir(pathname):
es68(pathname, estensioni)
return c
I expect the function to return me the number of times it encounters the extension. the problem is that every time the recursion is done the variable 'c' is reset to 0 and i don't know how to fix this. I would like the variable 'c' every time it increases by 1 to not reset
without changing the inputs of the function, it must be just dir and estensioni
CodePudding user response:
You can accumulate the result of the recursive call back into c
:
if os.path.isdir(pathname):
c = es68(pathname, estensioni)