0

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:\User\Username\Desktop\Statistic\12345678_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.

Tiger1982
  • 19
  • 4
  • 1
    See [Get a filtered list of files in a directory](https://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory). – jarmod May 19 '22 at 14:01
  • yes, using glob is the way to handle multiple files with similar names, maybe it will be worthwhile to use some regex too. – kovashikawa May 19 '22 at 14:03

1 Answers1

2

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().

Joshua Farina
  • 175
  • 1
  • 10
  • Hi Thanks so far, but it is not providing any results. I define the path with: `folder_path = 'C:\User\Username\Desktop\Statistic\''` and then: `[f for f in os.listdir(folder_path) if f.endswith("Reference.csv")][0]` But the console is not providing any file name nor an error and the file is existing in that folder. – Tiger1982 May 20 '22 at 10:16
  • Based on your comment, you have the folder path, and you have the filename, but you don't have the file name assigned to a variable. folder_path = 'C:\User\Username\Desktop\Statistic\' filename = [f for f in os.listdir(folder_path) if f.endswith("Reference.csv")][0] file_path = folder_path + filename Then feed the file path to the pandas function that you were using. – Joshua Farina May 24 '22 at 16:03