我試圖從標準輸出參數比較已知字符串我有, 所以,如果有兩個字符串之間的匹配,它會給我退出代碼0, 而在如果不匹配,則以退出代碼1結束。從標準輸出比較參數字符串
我試圖從函數輸出插入stdout參數,但我得到一個錯誤。
這是我使用的代碼:
import subprocess
from subprocess import check_output
def pwd():
pwdcmd = subprocess.call("pwd")
out = check_output([pwd()])
print "this is where you are --> " + out
從我讀過的和使用的命令相同的命令嘗試,而不是一個功能它的工作原理:
out = check_output(["pwd"])
print "this is where you are --> " + out
如何將stdout放入函數的「out」參數中?
這是我的錯誤:
**
/opt/sign
Traceback (most recent call last):
File "/opt/sign/test.py", line 15, in <module>
out = check_output([pwd()])
File "/usr/lib64/python2.7/subprocess.py", line 568, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
AttributeError: 'NoneType' object has no attribute 'rfind'
Process finished with exit code 1
**
你能給一個例子這意味着什麼*我想比較一個參數從標準輸出到已知的字符串我有,*。我在代碼中看不到任何比較結果。 –
@MikeMüller我的問題不在於比較,而是從我的函數stdout中獲取參數給參數。 –
我剛剛發佈的答案應該是你需要的。或者給你足夠的空間來做你在這個例子中做的其他事情。 –