我正在尝试编写一个Python程序,它可以与bsdgames trek程序进行交互。有点像Zork和Klingons:
* * * S T A R T R E K * * *
Press return to continue.
What length game: short
What skill game: fair
Enter a password: hunter2
10 Klingons
2 starbases at 3,6, 0,2
It takes 400 units to kill a Klingon
Command:
我目前正在尝试使用subprocess
import subprocess
import sys
proc = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE) #the cmd program opens
proc.communicate(input="filename.txt") #here the filename should be entered (runs)
#then the program asks to enter a number:
proc.communicate(input="1") #(the
我尝试了这个来解决我的问题,但是当使用这个解决方案时,我得到了以下错误:
Traceback (most recent call last):
File "version_build.py", line 125, in <module>
if __name__=='__main__': main()
File "version_build.py", line 113, in main
change = p4change(p4)
File "version_build.py", line 90
这是我的日志
File "/opt/ibm/db2-governor/helpers/utils.py", line 10, in run_cmd
output = proc.communicate(timeout = timeout)[0]
File "/opt/ibm/dynamite/python/lib/python2.7/site-packages/subprocess32.py", line 927, in communicate
stdout, stderr = self._communicate(input, endtime, tim
当我使用subprocess时,我通常可以捕获标准输出并随心所欲地显示它。例如,
import subprocess
proc = subprocess.Popen(['./foo.py'], stdin=subprocess.Pipe, stdout=subprocess.Pipe)
# the standard out is not displayed unless I do something with the stdout var
stdout, stderr = proc.communicate()
但是,如果我使用subprocess来调用Linux passwd实
根据这里的解决方案和python手册,下面的代码片段不能在屏幕上打印任何东西,包括cmd的stderr。
p = subprocess.Popen(cmd, cwd=tmpdir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
o, e = p.communicate()
但不知何故,我在终端上收到了cmd的错误消息。这种行为改变了吗?我尝试了下面的变体,它也有相同的结果:
p = subprocess.Popen(cmd, cwd=tmpdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE
我从Python2.6的模块中调用pipe.communicate。我从这段代码中得到以下错误:
from subprocess import Popen
pipe = Popen(cwd)
pipe.communicate( data )
对于任意的cwd,其中包含unicode的data (特别是0xE9):
Exec. exception: 'ascii' codec can't encode character u'\xe9' in position 507: ordinal not in range(128)
Traceback (most
我试图为自己编写一个简单的脚本来删除Windows点,我尝试使用os模块,然后使用os.system,但是它写的是vssadmin is not recognized,同时,我尝试使用subprocess,然后在cmd中使用communicate,但是我不能communicate两次,我不知道为什么。
有没有办法在Python中将下面的脚本运行到cmd中?
它将移除所有系统恢复点的阴影,然后问我是否想这样做。
vssadmin delete shadows /all
y
第一个示例:
它会抛出一个错误,因为我试图与命令提示符进行两次通信。
import subprocess
proc = s
因此,我正在编写(或至少尝试)一个程序来比较python中两个gdb运行的输出。这就是我到目前为止所知道的:
from subprocess import *
import subprocess
file = raw_input('enter program name (with a ./ if in local directory): ')
p1 = Popen(['gdb', file], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = Popen([