Tryinig to parse mutiple files in python using glob but I am confused on how to implement it

59 Views Asked by At

So I am very new to python so i tend to feel lost at times, I am working on a data science project where I need to parse different files to make different graphs, up until now I have read about glob function but I am not sure how to implement it, I would really appreciate the help. My file name goes something like this : PRA-DIA_8.1_A_1.stat0, PRA-DIA_8.1_A_1.stat1, PRA-DIA_8.1_A_1.stat3....

I have no issues with parsing a single file but when i want to try it for multiple files then i get lost. Thanks for the help

1

There are 1 best solutions below

7
arnaud On BEST ANSWER

If you want to find all files starting with PRA-DIA_8.1_A_1.sta, assuming you're already located within their directory, you can use the following (see the star operator, meaning basically "anything"):

import glob
file_paths = glob.glob("./PRA-DIA_8.1_A_1.sta*")

Now that you have all filenames, you can parse each one of them:

for file_path in file_paths:
    with open(file_path, "r") as f:
        file_content = f.read()
        ...  # do your supplementary parsing here