I am trying to concatenate csv files (with same headers) from folder in one df, but I get an empty list when reading the files:
from pathlib import Path
path = '//Users//Directory//'
files = Path(path + "dirtest//").glob('*.csv')
If I print file names, I get them right:
for f in files:
print(f.name)
filename1.csv
filename2.csv
filename3.csv
But when I try to read them and append sem into a list, it returns an empty list:
dfs = []
dfs = [pd.read_csv(f) for f in files]
dfs
[]
The next steps would be to concatenate the list into a df, but I couldn't get to it because the list is empty:
base_renov = pd.concat(dfs, ignore_index=True)
Can someone help me out?
Path.globreturns a generator. Once you print the values in the list, this exhausts the generator:You should directly pass this to
read_csv:Or, as a one-liner:
If you really want to use a loop (for example if you have other operations to perform), first convert the generator to list:
Or, creates
dfsin your loop: