【发布时间】:2014-02-16 19:11:21
【问题描述】:
我想将所有 npy/npz 文件加载到我的交互式 python shell 中,所以我不必去:
var1 = np.load('var1.npy')
每一个。我制作了这个脚本,但它不起作用,因为变量命名空间就在脚本中(假设缩进是正确的)。这样做的正确方法是什么?
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")
【问题讨论】:
标签: python numpy namespaces scope