Skip to content
Advertisement

I couldn’t make a list with these codes in python [closed]

I couldn’t make a list with these codes in python

  import os
path = r'C:UsersfabriDownloadsarquivos'
extension = '.csv'
for root, dirs_list, files_list in os.walk(path):
    for file_name in files_list:
        if os.path.splitext(file_name)[-1] == extension:
            file_name_path = os.path.join(root, file_name)
            print(file_name_path)

Advertisement

Answer

Use glob.glob() with the recursive=True option to get all the matching files in a folder and its subfolders.

from glob import glob
path = r'C:UsersfabriDownloadsarquivos'
file_list = glob.glob(os.path.join(path, '**/*.csv'), recursive=True)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement