我有这个密码:
netshcmd = subprocess.Popen('netsh wlan stop hostednetwork', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
output, errors = netshcmd.communicate()
if errors:
print("Warrning: ", errors)
else:
print("Success", output)
输出如下:
Success b'The hosted network stopped. \r\n\r\n'
如何获得这样的输出“成功托管网络停止”?你知道吗
那是一个副测试环。更改代码使其成为str:
从子进程中读取数据会给您一个bytestring。您可以对这个bytestring进行解码(必须找到合适的编码),或者使用
universal_newlines
选项,让Python自动为您解码:从Frequently Used Arguments documentation section:
对于通过shell运行的进程,
locale.getpreferredencoding(False)
应该是正确的要使用的编解码器,因为它从其他进程(如netsh
)应该参考的位置locale environment variables获取关于要使用什么编码的信息。你知道吗使用
universal_newlines=True
,output
将被设置为字符串'The hosted network stopped. \n\n'; note the newlines at the end. You may want to use
str.strip公司()`删除多余的空白:相关问题 更多 >
编程相关推荐