Skip to content
Advertisement

Reading csv file with partially variable name

I want to read a csv file into a data frame from a certain folder with pandas. This folder contains several csv files. They contain different information.

df = pd.read_csv(r'C:UserUsernameDesktopStatistic12345678_Reference.csv')

The first part in the filename (1 – 8 is variable). I want to read it in the file which ends with ‘_Reference.csv’, but I have no clue how to manage it. I googled, but could not find a solution if there are more than one csv file in the same folder.

Advertisement

Answer

If you import os, then you can use functions for for navigating the file system.

os.listdir(path) will return a list of all of the file names in a directory.

[f for f in os.listdir(path) if f.endswith("Reference.csv")]

Will return a list of all files names ending with “Reference.csv”. In your scenario, it sounds like there will be only one item in the list.

So, [f for f in os.listdir(path) if f.endswith("Reference.csv")][0] would return the filename that you’re looking for.

Then you can construct a path using the filename, and feed it to pd.read_csv().

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement