1

I need to read in multiple CSV files in the correct order. The files are named with sequential numbers, like "file_0.csv", "file_1.csv", "file_2.csv", ... and were created in this same order.

When running my code, the files are not kept in this order but instead completely mixed up. There are no other files in the path folder.

path = "stored_files"
filenames = glob.iglob(path + "/*.csv")
dataframes = []

for filename in filenames:
    dataframes.append(pd.read_csv(filename))
lapplapp
  • 51
  • 5

2 Answers2

1

AFAIK, glob provides randomly-ordered access to the folder. You can always sort fhe filenames:

path = "stored_files"
filenames = glob.iglob(path + "/*.csv")
dataframes = []

for filename in sorted(filenames):
    dataframes.append(pd.read_csv(filename))
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

String sort will not sort strings with numbers the way you expect (in particular, 10 precedes 2). So, if you know what your filenames look like, do loop through the numbers and append "foo"+str(i)+".csv" or whatever to your filelist.

Igor Rivin
  • 4,632
  • 2
  • 23
  • 35