Discuss / Python / 有问题!

有问题!

Topic source

Alvin天枭

#1 Created at ... [Delete] [Delete and Lock User]

如果子进程还需要输入,则可以通过communicate()方法输入:

import subprocess

print('$ nslookup') p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, err = p.communicate(b'set q=mx\npython.org\nexit\n') print(output.decode('utf-8')) print('Exit code:', p.returncode)

运行报错

Traceback (most recent call last): File "pt1_subprocess.py", line 27, in <module> print(output.decode('utf-8')) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 2: invalid continuation byte

###怎么解决呢?

Reva龙舞

#2 Created at ... [Delete] [Delete and Lock User]

window的环境下,cmd默认的字符集是gbk 把这一句 print(output.decode('utf-8')) 改成 print(output.decode('gbk')) 就行了


  • 1

Reply