Search code examples
pythoncsvpathexport-to-csv

Extract the csv files from the sub directory python


I have the local path (parent directory) and I would like to extract only the paths that contain csv and saves them in a csv file.

What I tried so far?

import os

directory = os.path.join("path/to/dir","path")
for root,dirs,files in os.walk(directory): 
    for file in files: 
        if file.endswith(".csv"): 
            f=open(file, 'r')
            f.close()

This does not extract all csv and saves it. How do I do that?


Solution

  • I think you don't really need to use os.walk function. Instead glob has the recursive functionality that can get you exactly what you want.

    from glob import glob
    import csv
    import os
    
    parent_directory = "/parent/directory/"
    save_file = "/save/directory/csv_of_csvs.csv"
    csv_files_list = glob(pathname=parent_directory + "**/*.csv", recursive=True)
    folder_list = [os.path.dirname(i) for i in csv_files_list]
    folder_list = set(folder_list)
    
    with open(save_file, 'w', newline ='') as csv_file:
        write = csv.writer(csv_file, delimiter=',')
        for i in folder_list:
            write.writerow([i])
    exit()