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")
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With