![](/img/trans.png)
[英]How can I get the python script path from a python interpreter command line?
[英]How can I get output from command line into condition in script?
我尝试了这个:
import os
if os.system('somecommand') == 'output from command':
do_something()
但这没有用。 我该怎么做? 我应该使用哪个库?
当我在下面的子流程中使用解决方案时,出现此错误代码:
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
os.system()
的返回值是os.system()
的退出代码,而不是输出到stdin的代码。 您要改为使用subprocess
模块 :
import subprocess
if subprocess.check_output('somecommand') == 'output from command':
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.