Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Python script to load all npy/npz files in a directory

I want to load all npy/npz files into my interactive python shell, so I don't have to go:

var1 = np.load('var1.npy')

for each one. I made this script, but it doesn't work because it the variables namespace is just within the script (pretend the indenting is correct). What is the right way to do this?

def load_all():
import numpy as np
from os import listdir
from os.path import isfile, join
from os import getcwd

mypath = getcwd()
print 'loading all .npy and .npz files from ',mypath
files = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]

for f in files:  
    if f[-4:] in ('.npy','.npz'):
        name = f[:-4]+'_'+f[-3:]
        print 'loading', f, 'as', name
        var = np.load(f)
        exec(name + " = var")
like image 866
capybaralet Avatar asked Oct 20 '22 16:10

capybaralet


1 Answers

I'd use glob. For example, glob.glob('*.np[yz]') will return a list of all .npy and .npz filenames in the current directory. You can then iterate over that list, loading each filename in turn. Are you trying to then put the results of loading them into local variables that match the filename? There are safer designs than that - I'd use a single dictionary and use the names as keys, something like:

numpy_vars = {}
for np_name in glob.glob('*.np[yz]'):
    numpy_vars[np_name] = np.load(np_name)
like image 106
Peter DeGlopper Avatar answered Oct 23 '22 07:10

Peter DeGlopper