我想从commande获取变量中的stdout输出 经过一些研究后我发现subprocess.check_output(cmd)做得很好!但它仍然在stdout打印出一些输出!
cmd = ["openssl","rsa", "-in","pubkey.pem","-pubin","-text","-modulus"]
output = subprocess.check_output(cmd)
我在输出变量中得到了我想要的但它仍然打印我
writing RSA key
并且在输出var i中有
'Public-Key: (576 bit)\nModulus:\n 00:c2:cb:b2:4f:db:f9:23:b6:12:68:e3:f1:1a:38:\n 96:de:45:74:b3:ba:58:73:0c:bd:65:29:38:86:4e:\n 22:23:ee:eb:70:4a:17:cf:d0:8d:16:b4:68:91:a6:\n 14:74:75:99:39:c6:e4:9a:af:e7:f2:59:55:48:c7:\n 4c:1d:7f:b8:d2:4c:d1:5c:b2:3b:4c:d0:a3\nExponent: 65537 (0x10001)\nModulus=C2CBB24FDBF923B61268E3F11A3896DE4574B3BA58730CBD652938864E2223EEEB704A17CFD08D16B46891A61474759939C6E49AAFE7F2595548C74C1D7FB8D24CD15CB23B4CD0A3\n-----BEGIN PUBLIC KEY-----\nMGQwDQYJKoZIhvcNAQEBBQADUwAwUAJJAMLLsk/b+SO2Emjj8Ro4lt5FdLO6WHMM\nvWUpOIZOIiPu63BKF8/QjRa0aJGmFHR1mTnG5Jqv5/JZVUjHTB1/uNJM0VyyO0zQ\nowIDAQAB\n-----END PUBLIC KEY-----\n'
pubkey.pem的内容是:
-----BEGIN PUBLIC KEY-----
MGQwDQYJKoZIhvcNAQEBBQADUwAwUAJJAMLLsk/b+SO2Emjj8Ro4lt5FdLO6WHMM
vWUpOIZOIiPu63BKF8/QjRa0aJGmFHR1mTnG5Jqv5/JZVUjHTB1/uNJM0VyyO0zQ
owIDAQAB
-----END PUBLIC KEY-----
Thanx寻求帮助:D
答案 0 :(得分:1)
check_output
允许您将stderr
重定向到文件描述符。我已将stderr
重定向到下面的'/ dev / null`,我认为它可以为您提供所需内容:
dev_null = open(os.devnull, 'w')
output = subprocess.check_output(cmd, stderr=dev_null)