急问:如果利用python多次执行一个程序,将不同结果放一个文件中

急问:如果利用python多次执行一个程序,将不同结果放一个文件中

大家好,
我利用python调用我linux下的软件进行计算,python calc.py后能够成功执行。
单独一个计算如下
calc.py内容如下:

import string, re, sys, os, time, math
calc = '/usr/local/bin/resc 1bmp.pdb 8 A > 8.txt '
os.system(calc)

上面的8是 参数, 8.txt是输出文件。如果进行其它计算,只需要修改参数8和输出文件8.txt

现在我想将参数分别换成1,2,3,...100,然后产生的结果输出到1.txt,2.txt,3.txt, ....100.txt。
如果单独计算需要算100次,所以必须写个程序让自动从参数1计算到100.


我试图这些写multcalc.py,但执行不对啊:

import string, re, sys, os, time, math
i = 1
abc = ''
while i <=100:
      abc = abc + i +'A' > i +'.txt'     
      calc = '/home/shulin/program/rescP/resc 1PGA.pdb  ' + abc
os.system(calc)

错误说TPYEError:can not concatenate 'str' and 'int' objects

请问如果修改才好另外最好输出的1.txt,2.txt,3.txt, ....100.txt 内容都直接放一个文件里。
非常感谢!!!
改成
calc = "/home/shulin/program/rescP/resc 1PGA.pdb  %d.txt"%(i)
试试
非常感谢reiase帮忙
我按照你的建议这样修改的:
import string, re, sys, os, time, math
i= 1
while i <= 56:
      calc = "/home/shulin/program/rescP/resc 1PGA.pdb %d.txt"%(i)
      i = i + 1
os.system(calc)

出错信息:The first argument should bu numerical
我反复尝试其他方法还是不行。不知道如何是好


[Copy to clipboard] [ - ]
CODE:
import string, re, sys, os, time, math
i= 1
while i <= 56:
      calc = "/home/shulin/program/rescP/resc 1PGA.pdb %d.txt"%(i)
      i = i + 1
      print calc

我这里没问题阿

[Copy to clipboard] [ - ]
CODE:
/home/shulin/program/rescP/resc 1PGA.pdb 1.txt
/home/shulin/program/rescP/resc 1PGA.pdb 2.txt
/home/shulin/program/rescP/resc 1PGA.pdb 3.txt
/home/shulin/program/rescP/resc 1PGA.pdb 4.txt
/home/shulin/program/rescP/resc 1PGA.pdb 5.txt
/home/shulin/program/rescP/resc 1PGA.pdb 6.txt
/home/shulin/program/rescP/resc 1PGA.pdb 7.txt
/home/shulin/program/rescP/resc 1PGA.pdb 8.txt
/home/shulin/program/rescP/resc 1PGA.pdb 9.txt
...

还可以使用str.
reiase,非常感谢,我重新试过了:
import string, re, sys, os, time, math
i= 1
while i <= 10:
      calc = "/home/shulin/program/rescP/resc 1PGA.pdb %d.txt"%(i)
      i = i + 1
      print calc
      os.system(calc)

程序产生以下结果
print没有问题,但os.system无法执行calc了,我估计calc被当成了个字符串了。


[shulin@fishmac rescp]$ python 5.py
/home/shulin/program/rescP/resc 1PGA.pdb 1.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 2.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 3.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 4.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 5.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 6.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 7.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 8.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 9.txt
The first argument should be numerical
/home/shulin/program/rescP/resc 1PGA.pdb 10.txt
The first argument should be numerical
>>> import os
>>> i=1
>>> while i<5:
...     calc = 'echo %d > c:/%d.txt'%(i,i)
...     i += 1
...     os.system(calc)
reiase, 现在我这样可以执行程序了,但无法保存结果:

import string, re, sys, os, time, math
i= 1
while i <= 10:
      calc = "/home/shulin/program/rescP/resc 1PGA.pdb %d A"%(i)
      i = i + 1
      print calc
      os.system(calc)

这样可以正确执行了,但要是保持结果.
这些结果只能输出在屏幕上,比较麻烦。
calc = "/home/shulin/program/rescP/resc 1PGA.pdb %d A > out.txt"%(i)

如果这样每个计算结果都近如out.txt文件,但前面的结果被后面的覆盖了,结果只有最后一个参数的结果了。
非常感谢你啊,reiase,现在就差一步就解决了。
不好意思,给你添麻烦了
其实大家都不很清楚你的命令怎么调用,我们只是示范怎么用格式化字符串
试试
"/home/shulin/program/rescP/resc 1PGA.pdb %d A > %d.txt"%(i,i)