免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6235 | 回复: 10
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-30 05:26 |只看该作者 |倒序浏览
大家好,
我利用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 内容都直接放一个文件里。
非常感谢!!!

论坛徽章:
0
2 [报告]
发表于 2008-07-30 07:47 |只看该作者
改成
calc = "/home/shulin/program/rescP/resc 1PGA.pdb  %d.txt"%(i)
试试

论坛徽章:
0
3 [报告]
发表于 2008-07-30 08:07 |只看该作者

回复 #2 reiase 的帖子

非常感谢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
我反复尝试其他方法还是不行。不知道如何是好

论坛徽章:
0
4 [报告]
发表于 2008-07-30 08:22 |只看该作者

  1. import string, re, sys, os, time, math
  2. i= 1
  3. while i <= 56:
  4.       calc = "/home/shulin/program/rescP/resc 1PGA.pdb %d.txt"%(i)
  5.       i = i + 1
  6.       print calc

复制代码

我这里没问题阿

  1. /home/shulin/program/rescP/resc 1PGA.pdb 1.txt
  2. /home/shulin/program/rescP/resc 1PGA.pdb 2.txt
  3. /home/shulin/program/rescP/resc 1PGA.pdb 3.txt
  4. /home/shulin/program/rescP/resc 1PGA.pdb 4.txt
  5. /home/shulin/program/rescP/resc 1PGA.pdb 5.txt
  6. /home/shulin/program/rescP/resc 1PGA.pdb 6.txt
  7. /home/shulin/program/rescP/resc 1PGA.pdb 7.txt
  8. /home/shulin/program/rescP/resc 1PGA.pdb 8.txt
  9. /home/shulin/program/rescP/resc 1PGA.pdb 9.txt
  10. ...
复制代码

论坛徽章:
0
5 [报告]
发表于 2008-07-30 08:41 |只看该作者
还可以使用str.

论坛徽章:
0
6 [报告]
发表于 2008-07-30 08:48 |只看该作者

回复 #4 reiase 的帖子

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

论坛徽章:
0
7 [报告]
发表于 2008-07-30 08:58 |只看该作者
>>> import os
>>> i=1
>>> while i<5:
...     calc = 'echo %d > c:/%d.txt'%(i,i)
...     i += 1
...     os.system(calc)

论坛徽章:
0
8 [报告]
发表于 2008-07-30 09:02 |只看该作者
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)

这样可以正确执行了,但要是保持结果.

论坛徽章:
0
9 [报告]
发表于 2008-07-30 09:06 |只看该作者
这些结果只能输出在屏幕上,比较麻烦。
calc = "/home/shulin/program/rescP/resc 1PGA.pdb %d A > out.txt"%(i)

如果这样每个计算结果都近如out.txt文件,但前面的结果被后面的覆盖了,结果只有最后一个参数的结果了。
非常感谢你啊,reiase,现在就差一步就解决了。
不好意思,给你添麻烦了

论坛徽章:
0
10 [报告]
发表于 2008-07-30 09:09 |只看该作者
其实大家都不很清楚你的命令怎么调用,我们只是示范怎么用格式化字符串
试试
"/home/shulin/program/rescP/resc 1PGA.pdb %d A > %d.txt"%(i,i)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP