python调用exe,希望高手指点。
如题,这个问题并不新鲜,但是我确实搞不明白了,这次。
一个exe程序,我在cmd下可以运行(运行时候不需要任何参数输入),找到exe路径,敲回车就行。正常输出结果。但是得等一会才会输出结果(输出一个txt文本),因为本身exe运行起来需要2分钟时间。
但是我在用python调用的时候就是没反应啊。我的代码这样:
- Python code
exe_1=dstDir+'interpolation.exe' print exe_1 os.popen(exe_1)
其中,dstDir=‘D:\Fortran\users\Ace\2011-12-06-10-17-16\’
exe_1打印出来是‘D:\Fortran\users\Ace\2011-12-06-10-17-16\interpolation.exe’
我用的是需要一个变量的文件夹路径,我用的是os.popen。python的shell也没有报错,但就是没有txt的结果输出。
我改成os.system(exe_1)后,cmd黑框闪了一下,之后也没有结果输出。
请问这是咋回事啊?
之前我也做过python调用exe的东西啊,没有问题啊当时。
如:
os.popen('D:\DBS\EXE\DBSoTOraw.exe %s %s %s' %(a[0].decode('utf-8'),'D:\DBS\wu.raw',threshold))
这个还传递参数呢。
我就是把他写成了绝对路径,如:os.system('D:\Fortran\users\Ace\2011-12-06-10-17-16\interpolation.exe’)
也有问题,闪一下黑框,无txt结果输出。
着急啊。。。咋回事。。。
= =!
[解决办法]
- Python code
[root@RHEL6B pycode]# ./py13.py not set: [root@RHEL6B pycode]# vim py13.py [root@RHEL6B pycode]# ./py13.py Success:this is test text.[root@RHEL6B pycode]# cat py13.py #!/usr/bin/env python#coding:utf-8from subprocess import Popen, PIPEreturn_code=-999 proc = Popen(['/root/shcode/sleep.sh'], stdout=PIPE, stderr=PIPE)return_code = proc.wait()voutput=proc.stdout.read()if return_code == 0: print "Success:\n%s" % voutput elif return_code == -999: print 'not set:', voutputelse: print "Failure %s:\n%s" % (return_code, voutput)[root@RHEL6B pycode]# cat /root/shcode/sleep.sh #!/bin/bashsleep 10echo this is test text.[root@RHEL6B pycode]#
[解决办法]
你system那句里的字串参数,\201会被转义,老实点一律用\\或者/吧。
[解决办法]