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]…..
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.