Retrieving files and storing them separately into two different lists

I am trying to retrieve files from a folder called ‘dataframes’ and store them into a list by separating them via there station identifier in the filename. However, every time I do it, the lists are empty. Any help would be great.

list1 = []
list2 = []

def get_station(file_name: str):
    return re.findall(r'.\d\d.', file_name)

def main():
    for filename in os.listdir('dataframes'):
       file_path = os.path.join('dataframes', filename)
       station = get_station(filename)
       if station == '23':
           list1.append(file_path)
       elif station == '05':
           list2.append(file_path)
    print(list1)
        


                
if __name__ == "__main__":
    main()

re.findall returns a list, and a list is never equal to a string.

2 Likes

When code does not do what you expect it is worth adding print statements to see what is happening.

In this case a print(station) in the loop would show that it is not a string as @MRAB figured out.

1 Like

So how can I find the station and use it to separate stations into two different lists?

The list contains all of the substrings that are: something, digit, digit, something.

I assume that you’re expecting only 1 match, so pick it. It’ll be in station[0].

Actually, it won’t be just the 2 digits, it’ll the 4 characters that matched, so you’ll need to slice off the unwanted bits: station[0][1 : 3] .

Add print statements while you’re debugging so that you can see what it’s doing, as @barry-scott said.

Copy, thanks