循环用于解释python脚本(.py)

时间:2013-07-22 07:14:17

标签: unix python-2.7

我是Python的初学者。我的问题可能是基本的,但我无法弄清楚。我想要做的是运行以下命令100次,其中变量“num”从0到99运行。

python test.py input_num.txt -i 5 --cor_file = output_num.txt

例如:

python test.py input_0.txt -i 5 --cor_file = output_0.txt

python test.py input_1.txt -i 5 --cor_file = output_1.txt

...    :::

python test.py input_99.txt -i 5 --cor_file = output_99.txt

我知道我必须编写一个用于运行循环的脚本,但还是无法搞清楚。如果您有任何建议,那将是非常好的。

我尝试编写一个名为main.py的脚本,其中包含以下命令,但它不起作用。

#!/usr/bin/env python

import test
ext_str = '.txt'
inp = 'input_'
out = 'output_'

for num in range(0,99):    
  inp += 'num'
  inp_str = inp + ext_str

  out += 'num'
  out_str = out + ext_str

  python test.py inp_str -i 5 --cor_file=out_str

非常感谢。

最佳, 皮姆

1 个答案:

答案 0 :(得分:0)

在你的情况下,你需要在各种文件上调用python解释器,例如你需要调用“python”shell命令,这意味着你必须创建一个shell脚本(而不是python脚本,实际上你可以用一些python代码来实现它,但在你的情况下它会太复杂了):< / p>

for i in {0..99}
do
    python test.py input_$i.txt -i 5 --cor_file=output_$i.txt
done

将上面的代码放在“.sh”文件中,然后不要忘记“chmod + x the”,然后用./.sh运行它(假设你在当前目录下)。

N.B。:如果你需要关于bash脚本的一些参考:Linux Shell Scripting Tutorial