KSQ
KSQ

Reputation: 191

Python: Importing files and creating a list for each file

I am looping over a list containing 9 file names and trying to create 9 variables (i.e. one for each file that I open).

The code seems to import correctly but I cant get it to create 9 variables, I've only managed to create one.

Code that works and creates one list (the 9th file):

for i in category_list:
    j = category_list.index(i)
    with open(str(path) + category_list[j] + f_ext, 'rb') as f:
        d = pickle.load(f)

Code that gives me error "name 'd_' is not defined

for i in category_list:
    j = category_list.index(i)
    with open(str(path) + category_list[j] + f_ext, 'rb') as f:
        d_[i] = pickle.load(f)

I think I may need to either declare the variable (which doesn't feel right for python) or I'm missing something even more simple.

Appreciate any help.

Thanks.

Upvotes: 0

Views: 51

Answers (1)

Vasyl Kolomiets
Vasyl Kolomiets

Reputation: 441

hm... it seams it may works:

d_ = []
for category in category_list:
    with open(str(path) + category + f_ext, 'rb') as f:
        d_.append( pickle.load(f))

Isn't it? If - it is so - try eat more Python-like code )

Upvotes: 1

Related Questions