I have text files in a directory with data. I am looking at populating a dict with keys being the name of the text files, and the corresponding items containing the data of the files. I would like the order of the name keys in the dict to reflect the order of creation of the test files in the repertory. The code I used is:
date_file_list = []
for filename in files:
stats = os.stat(filename)
lastmod_date = time.localtime(stats[8])
date_file_tuple = lastmod_date, filename
date_file_list.append(date_file_tuple)
date_file_list.sort()
date_file_list_array = np.asarray(date_file_list)
File2Array = {}
for filename in date_file_list_array[0:20,1]:
print(filename)
File2Array.setdefault(filename,OpenandReadOO(filename))
OpenandReadOO is a python function that open and read the file with name filename and fill a numpy array.
Unfortunately, even if list date_file_list and date_file_list_array are correctly ordered, when iterating through them (seemingly in the right order), the resulting dict is always not ordered.
How should I proceed to obtain a dict with items (and data) in the correct order?
Thanks
Greg