如何定义脚本从哪里加载文件?

How to define from where the script loads files?

我使用脚本的一部分从注册文件加载数据 .npz

Here is the code:

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']

那么我的问题是什么?现在,当我处于所描述的第二阶段时,它会从脚本所在的文件夹中一个接一个地加载文件。 我想以我可以选择的方式对其进行编码(打开 window,或者在代码中写一些带有完整地址的内容)它将加载文件的位置(所有文件总是在同一个文件夹)

这是背景:我将把我的数据存储在一个不适合工作的硬盘上,所以我不能在上面安装 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))
    ...