如何使用子目录的数据文件并在Python中执行迭代操作
我在当前目录中有我的jupyter笔记本(Python脚本)。在当前目录中,我有两个子文件夹,即a
和b
。在两个目录中a
和b
我的.dat
文件具有相同名称。例如,目录a
包含文件,即x1-x1-val_1
,x1-x1-val_5
,x1-x1-val_11
... x1-x1-val_86
和x1-x2-val_1
,x1-x2-val_5
,x1 -x2-val_11
... x1-x2-val_86
,IE值在范围内(1,90,5)
。同样,我有目录中的文件b
。
我想使用我的python脚本访问a
和b
中的文件,以在.dat
文件上执行迭代操作。我现在的代码仅在当前目录中保存目录 a a a a b 的文件时才有效。例如,我的脚本使用以下函数。
def get_info(test):
my_dict = {'test':test}
c = []
for i in range(1,90,5):
x_val = 'x_val_'+test+'-val_'+str(i)
y_val = 'y_val_'+test+'-val_'+str(i)
my_dict[x_val],my_dict[y_val]= np.loadtxt(test+'-val_'+str(i)+'.dat'
,usecols= (1,2),unpack=True)
dw = compute_yy(my_dict[x_val],my_dict[y_val],test)
c.append(dw)
my_dict.update({test+'_c'+:np.array(c)})
return my_dict
我调用get_info()
通过使用以下方式:
tests = ['x1-x1', 'x1-x2']
new_dict = {}
for i in tests:
new_dict.update({i:get_info(i)})
如何使用代码在Directory a
和/或b
中访问文件?我知道这是关于提供正确的道路,但我不确定该怎么做。我想的一种方法是遵循;
ext = '.dat'
for files in os.listdir(path_to_dir):
if files.endswith(ext):
print(files) # do operations
替代方法可能是使用os.path.join()
。但是,我无法解决它,因此我可以使用相同的python脚本(也许有最小的更改)可以使用文件并在子文件夹中迭代a
a and b 。感谢您提前的反馈!
I have my jupyter notebook (python script) in current directory. In current directory, I have two subfolders, namely a
and b
. In both directories a
and b
I have equal number of .dat
files with same names. For example, directory a
contains files, namely x1-x1-val_1
, x1-x1-val_5
, x1-x1-val_11
...x1-x1-val_86
and x1-x2-val_1
, x1-x2-val_5
, x1-x2-val_11
...x1-x2-val_86
, i.e. values are in range(1,90,5)
. Likewise I have files in directory b
.
I want to use my python script to access files in a
and b
to perform iterative operations on .dat
files. My present code works only if I keep files of directory a
or b
in current directory. For example, my script uses following function.
def get_info(test):
my_dict = {'test':test}
c = []
for i in range(1,90,5):
x_val = 'x_val_'+test+'-val_'+str(i)
y_val = 'y_val_'+test+'-val_'+str(i)
my_dict[x_val],my_dict[y_val]= np.loadtxt(test+'-val_'+str(i)+'.dat'
,usecols= (1,2),unpack=True)
dw = compute_yy(my_dict[x_val],my_dict[y_val],test)
c.append(dw)
my_dict.update({test+'_c'+:np.array(c)})
return my_dict
I call get_info()
by using following:
tests = ['x1-x1', 'x1-x2']
new_dict = {}
for i in tests:
new_dict.update({i:get_info(i)})
How can I use my code to access files in either directory a
and/or b
? I know its about providing correct path, but I am unsure how can I do so. One way I thought is following;
ext = '.dat'
for files in os.listdir(path_to_dir):
if files.endswith(ext):
print(files) # do operations
Alternative could be to make use of os.path.join()
. However, I am unable to solve it such that I can use same python script (with minimum changes perhaps) that can use files and iterate on them which are in subfolders a
and b
. Thanks for your feedback in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在每个文件夹sameatelly上运行
get_info()
,则有两种方法:首先:由 @Medium-difimensional在评论中描述,
您可以使用
os。 CHDIR(文件夹)
更改当前工作目录
。然后,代码将在此文件夹中使用文件运行,中查看当前工作目录
您可以在
print(os.getCwd())
chdir()
(类似于命令cd)
在控制台中)可以使用相对路径(r“ a“
)完整路径(r“ c:\ full \ path \ to \ a”
)和<代码> .. 移动到父文件夹(r“ a \ .. \ b”
)如果文件可以在嵌套文件夹中,则可能不会回去您可以使用
getcwd()
(btw:在Linux上的控制台您可以使用
cd-
移动到以前的文件夹)second:当您打开文件时,请使用文件夹,
每个获取文件名的命令也可以使用
文件夹\ filename
获得路径(可以是相对路径,完整路径和..
)喜欢r“ a \ filename.dat”
r“ c:\ full \ path \ to \ b \ filename.dat”
r“ a \ .. \ b \ filename.dat“
,因此您可以使用Extra option
文件夹
定义函数,并在读取文件
或使用
f-string
的更读取时使用此文件夹,然后再使用此文件 夹。将其运行为
If you want to run
get_info()
on every folder separatelly then you have two methods:First: described by @medium-dimensional in comment
You can use
os.chdir(folder)
to changeCurrent Working Directory
. And then code will run with files in this folderYou can see current working directory with
print( os.getcwd() )
chdir()
(similar to commandcd
in console) can use relative path (r"a"
) full path (r"C:\full\path\to\a"
) and..
to move to parent folder (r"a\..\b"
)If files can be in nested folders then
..
may not go back you can usegetcwd()
(BTW: in console on Linux you can use
cd -
to move back to previous folder)Second: use folder when you open file
Every command which gets filename can also get path with
folder\filename
(it can be relative path, full path, and path with..
) liker"a\filename.dat"
r"C:\full\path\to\b\filename.dat"
r"a\..\b\filename.dat"
So you could define function with extra option
folder
and use this folder when you read file
or more readable with
f-string
And later you run it as