how to read through multiple .tsv files located in different sub-directories

Viewed 259

I have multiple .tsv files located in a directory located in a sub-directory w/ different names (sub-directory different names)

I'm trying to read each of the .tsv files and perform this command:

df_1 = pd.read_csv("C:/Car/0NN/car.tsv", delimiter='\t', encoding="utf-8-sig")
for node1 in df_1['#node1']:
    for node2 in df_1['node2']:
        if node1!=node2:
            df_temp = df_1.iloc[0:1,1:2]

Is there a way to modify the first line so I can loop through all files ending that are "car.tsv"? The folder "0NN" change names, but the .tsv file itself has the same name, and the main "Car" folder has the same name. Thank you

Ex:

C:/Car/0NN/car.tsv

C:/Car/1AP/car.tsv

1 Answers

You can use python's inbuilt glob module to recursively read all the files inside the directory. Assuming every file is named car.tsv and is inside any subdirectory of C:/Car/

all_car_tsv_files = glob.glob("C:/Car/**/car.tsv", recursive=True)

**/ : This is glob star which matches everything in its path, combined with / matches any subdirectory.

recursive = True : Makes sure to iterate over sub-directories inside sub-directories. For example, C:/Car/1AP/AP/car.tsv or C:/Car/1AP/AP/BP/CP/car.tsv

Related