[英]how to execute linux command in python which changes default prompt
我正在尝试使用一些更改Linux提示符的命令来执行python脚本。
>>> import os
>>> import commands
>>> os.chdir("/home/mycmd")
>>> commands.getoutput("pkill mycmd")
''
>>> commands.getoutput("mycmd") # it was stuck, had to press contrl^c
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/commands.py", line 50, in getoutput
return getstatusoutput(cmd)[1]
File "/usr/lib64/python2.7/commands.py", line 60, in getstatusoutput
text = pipe.read()
KeyboardInterrupt
手动执行mycmd
,看到的是(shell)
提示符,而不是#
提示符。 看起来与commands.getoutput不支持,有什么办法可以使用python脚本执行此类命令以更改提示?
您可以使用子过程执行此操作:
import subprocess
p = subprocess.Popen(['pkill', 'mycmd'], stdout=subprocess.PIPE, stderr=stdout)
p.communicate()
该模块打算替换几个较旧的模块和功能:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.