pythonpandascsvimportimport-from-csv

Reading a CSV file without the full name


Very new to Python here. I am trying to read a file from a folder, but without knowing its full name. For example AAA_05212021.csv is the file name located in C:\test\ AAA is the known part of the file name. The rest changes everyday.

I tried:

import pandas as pd
data = pd.read_csv(r'C:\test\AAA*.csv')

.. but it doesn't seem to work. Any help would be much appreciated. Thanks!


Solution

  • You can use glob for this. Glob will return a list of files that match the pattern. Since, there is only one file that matches the pattern in the directory, you can get it with the index 0:

    from glob import glob
    import pandas as pd
    
    file = glob('C:\test\AAA*.csv')[0]
    data = pd.read_csv(file)