import subprocess
def runcmd(command, cwd, timeout=5, shell=True, cancer_warp=False):
try:
p = subprocess.Popen(command, bufsize=-1, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
encoding="gbk", cwd=cwd)
p.wait(5)
returncode = p.returncode
stdout, stderr = p.communicate()
if cancer_warp:
stdout = stdout.split(('\n'))
result = {
"returncode": returncode,
"stdout": stdout,
"stderr": stderr,
}
return result
except Exception:
pass
if __name__ == '__main__':
result=CmdUtil.runcmd(command="dir", cwd="C:\",cancer_warp=True)
print(result)
遇到的报错(改一下字符集编码就可以了):
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation byte
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)