reading csv files with specific name in python

Viewed 42

I have some csv files with name pattern below

IU_2022-09-01T09_43_56_0100-0018-0000-0002

Bold part of the name is important and i want to create different data frames if that changes but i am not able to read csv files by specifying something in the middle of the name. i want to read only those csv files which has 0100 in their names. i used glob method

ls_data = list() 

for idx, f in glob.glob('[0100]*.csv'):

df_temp = pd.read_csv(f, delimiter=';')

df_temp["layer_number"] = idx

ls_data.append(df_temp)

print (idx)

df_L = pd.concat(ls_data, axis=0)

`

but i am getting empty data

1 Answers

Use pathlib :

from pathlib import Path
import pandas as pd

ls_data = []

csv_directory = r'/path/to/csvfiles/'

for idx, filename in enumerate(Path(csv_directory).glob('*_0100-*.csv')):
    df_temp = pd.read_csv(filename, delimiter=';')
    df_temp.insert(0, 'layer_number', idx)
    ls_data.append(df_temp) 

df = pd.concat(ls_data, axis=0)
Related