-1

I got a folder with 100 *.sav files, I have to load all in to a list, file names saved as my_files_0,my_files_1,my_files_2,my_files_3....

So, I tried:

import dill
my_files = []
files_name_list = glob.glob('c:/files/*.sav')
for i in np.arange(1, len(files_name_list)):
    with open('c:/files/my_files_%i.sav'%i, 'rb') as f: my_files = dill.load(f)
my_files

Here my_files list has only one file in it. I need all 100 files in the list, so I can access them based on index my_files[0]…..

hanzgs
  • 1,498
  • 17
  • 44
  • Is this has to be done using dill? – Vishwas Jul 15 '19 at 02:40
  • yes, its modelling files, joblib and pickle not working, so I am using dill, but if there is an option I can try, but I have to reproduce the error if I save as joblib or pickle – hanzgs Jul 15 '19 at 02:42

2 Answers2

2

The reason you only have one file in my_files is that you are continually overwriting the last result. I think what you meant to do was append the new result each time you iterate over your list:

import dill
my_files = []
files_name_list = glob.glob('c:/files/*.sav')
for i in np.arange(1, len(files_name_list)):
    with open('c:/files/my_files_%i.sav'%i, 'rb') as f:
        my_files.append(dill.load(f))

Also, note in your solution, you don't need to do a f.close() when you are using with to open f -- it's closed when it leaves the with block.

Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
0

I used array instead of list, it worked

file_array=[0]*len(files_name_list)
for i in np.arange(0, len(file_name_list)):
    with open('c:/files/my_files_%i.sav'%i, 'rb') as f: 
        file_array[i] = dill.load(f)
        f.close()
hanzgs
  • 1,498
  • 17
  • 44