0

I have a number of 1500 .txt files in a folder and each file has only one line like this:

24.171683 25.663069 26.996733 27.684257 28.411782 29.253564

The name of files is the combination of numbers and letters such as Animation1000_1_0.txt, Animation1000_10_1.txt, ... Now I need to read all .txt files' content in an array, but based on their order in the original folder. For this I used the following code:

psnr_bitrate=np.vstack([np.loadtxt(path, dtype='float') for path in glob.iglob(r'E:/PSNR_RDCURVE/*.txt')])

but when I checked the psnr_bitrate array, it does not have the same order as files and I do not know how this code read the files. Do you know what should I read the files in an array with same order as files?

Yun
  • 3,056
  • 6
  • 9
  • 28
david
  • 1,255
  • 4
  • 13
  • 26

1 Answers1

1

Have you tried sorted statement, like described here? Reading files in a particular order in python

for path in sorted(glob.iglob(r'E:/PSNR_RDCURVE/*.txt'))
Alex
  • 815
  • 9
  • 19
  • 1
    This will not result in a numerical order. It will be [lexicographic](https://stackoverflow.com/questions/45950646/what-is-lexicographical-order) instead, file_10 will be listed before file_2. The link you quote has a very good answer how to produce numerical order. – atru Sep 25 '21 at 12:13