Python 如何定义脚本从何处加载文件?
我使用脚本的一部分从注册文件Python 如何定义脚本从何处加载文件?,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我使用脚本的一部分从注册文件.npz加载数据 这是代码: Tk().withdraw() # Here starts the first loading phase, where I pick the file I want from a window filename = askopenfilename() with load(filename) as data: # file loading logic here pass ext = '.npz' for i in ran
.npz
加载数据
这是代码
:
Tk().withdraw() # Here starts the first loading phase, where I pick the file I want from a window
filename = askopenfilename()
with load(filename) as data:
# file loading logic here
pass
ext = '.npz'
for i in range(1, NF): # Here starts the second part, which loads one by one from the folder where the script is.
filename = str(i) + ext
with load(filename) as data:
XYsliceTemp = data['XYslice']
那么我的问题是什么?现在,当我处于所描述的第二阶段时,它会从脚本所在的文件夹中逐个加载文件。
我想用一种我可以选择的方式(打开一个窗口,或者在代码中写一些完整的地址)来编写代码,以便加载文件(所有文件始终在同一个文件夹中)
这就是背景:我将把我的数据存储在一个硬盘上,而这个硬盘不是用来工作的,所以我不能在上面安装python并从那里运行它。
所以我想告诉我电脑上的脚本:把这些文件放在硬盘上的这个确切位置
实际上,第一阶段加载文件0,然后第二阶段从1加载到N。因此,如果我可以说:在我选择加载0的地方,去那里找到其他N个,那就太完美了。使用os.path.split()和os.path.join()方法:
非常感谢。这似乎既方便又容易。我会尽快试试这个。
import os
filename = askopenfilename()
directory = os.path.split(filename)[0]
ext = 'npz'
for i in range(1, NF):
filename = os.path.join(directory, '%s.%s' % (i, ext))
...