0

i need to ask if possible and how get on same time the sum of list files in directory and subdirectory with fnmatch filter and files adress.

I use for now this:

def return_ext():
    file_pasok = ["AAAAA.txt", "BBBBBB.txt"]
    for i in range(len(file_pasok)):
        for ext_f in file_pasok:
            return ext_f


def list_files(file_path):
    ext = return_ext()
    for _, dirnames, filenames in os.walk(file_path):
        if not filenames:
            continue

        for file in fnmatch.filter(filenames, ext):
            file_found_str = Path(os.path.join(_, file))
            file_found = str(file_found_str)
            yield file_found


ext = return_ext()

########GOT HOW MANY FILE FOUND

count_founded = sum([len(fnmatch.filter(files, ext)) for r, d, files in os.walk(file_path)])

########GOT LIST ADRESS FILE FOUND
for file_found in list_files(file_path):
    print(file_founds) 

But of course the script make 2 time the same search :( Thanks so much for any suggest !!

Massimo B.
  • 23
  • 6

1 Answers1

0
def return_ext():
    file_pasok = ["AAAA.txt", "BBBB.txt"]
    for ext_f in file_pasok:
        yield ext_f


def list_files(file_path):
    found_ext = return_ext()
    exts = list(found_ext)
    for _, dirnames, filenames in os.walk(file_path):
        if not filenames:
            continue
        for ext in exts:
            for file in fnmatch.filter(filenames, ext):
                if any(fnmatch.fnmatch(file, ext) for ext in file):
                    file_found_str = Path(os.path.join(_, file))
                    file_found = str(file_found_str)
                    yield file_found


converted_list = list_files(file_path)
count_found = list(converted_list)
print(len(count_found))
for file_found in count_found:
    print(file_found)   
Massimo B.
  • 23
  • 6