编写一个调用不同目录的python脚本(writing a python script that calls different directories)

编程入门 行业动态 更新时间:2024-10-22 11:03:20
编写一个调用不同目录的python脚本(writing a python script that calls different directories) python

这有点难以解释,但我使用的目录有许多不同的文件但基本上我想循环不规则间隔的文件

所以在伪代码中我猜它会写成:

A = 1E4, 1E5, 5E5, 7E5, 1E6, 1.05E6, 1.1E6, 1.2E6, 1.5E6, 2E6 For A in range(start(A),end(A)): inputdir ="../../../COMBI_Output/Noise Studies/[A] Macro Particles/10KT_[A]MP_IP1hoN0.0025/"

运行其余代码

因为目前我正在通过改变[A]的值来手动完成它,这是一个噩梦和耗时。 我在macbook上使用Python ,所以我想知道编写一个在Python调用的bash脚本是否是正确的想法?

或者用文本文件替换A ,以便:

import numpy as np mpnum=np.loadtxt("mp.txt") for A in range(0,len(A)): for B in range(0,len(A)): inputdir ="../../../COMBI_Output/Noise Studies/",[A] "Macro Particles/10KT_",[A]"MP_IP1hoN0.0025/"

但我先尝试了这个,但仍然没有运气。

It's kind of hard to explain but I'm using a directory that has a number of different files but essentially I want to loop over files with irregular intervals

so in pseudocode I guess it would be written like:

A = 1E4, 1E5, 5E5, 7E5, 1E6, 1.05E6, 1.1E6, 1.2E6, 1.5E6, 2E6 For A in range(start(A),end(A)): inputdir ="../../../COMBI_Output/Noise Studies/[A] Macro Particles/10KT_[A]MP_IP1hoN0.0025/"

Run rest of code

Because at the moment I'm doing it manually by changing the value in [A] and its a nightmare and time consuming. I'm using Python on a macbook so I wonder if writing a bash script that is called within Python would be the right idea?

Or replacing A with a text file, such that its:

import numpy as np mpnum=np.loadtxt("mp.txt") for A in range(0,len(A)): for B in range(0,len(A)): inputdir ="../../../COMBI_Output/Noise Studies/",[A] "Macro Particles/10KT_",[A]"MP_IP1hoN0.0025/"

But I tried this first and still had no luck.

最满意答案

你快到了。 您不需要范围,只需遍历列表即可。 然后使用格式替换字符串。

A = ['1E4', '1E5', '5E5', '7E5', '1E6', '1.05E6', '1.1E6', '1.2E6', '1.5E6', '2E6'] for a in A: inputdir = "../../../COMBI_Output/Noise Studies/{} Macro Particles/10KT_{}MP_IP1hoN0.0025/".format(a)

You are almost there. You don't need a range, just iterate over the list. Then do a replacement in the string using format.

A = ['1E4', '1E5', '5E5', '7E5', '1E6', '1.05E6', '1.1E6', '1.2E6', '1.5E6', '2E6'] for a in A: inputdir = "../../../COMBI_Output/Noise Studies/{} Macro Particles/10KT_{}MP_IP1hoN0.0025/".format(a)

更多推荐

本文发布于:2023-08-07 18:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   目录   python   writing   calls

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!