与powersh一起使用时,Python子进程返回的格式错误

2024-04-24 07:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试以下方法:

   process=check_output(["powershell.exe", "Get-Childitem C:\\Windows\\*.log"]);
           return process

它返回时输出:

b'\r\n\r\n Directory: C:\Windows\r\n\r\n\r\nMode LastWriteTime Leng th Name \r\n---- ------------- - ----- ---- \r\n-a---- 6/14/2018 11:26 AM 4056 DtcInstall.log \r\n-a---- 6/21/2018 11:33 AM 41261 iis.log \r\n-a---- 9/12/2016 4:34 AM 1342 lsasetup.log \r\n-a---- 6/25/2018 12:06 PM 8904 PFRO.log \r\n-a---- 6/21/2018 12: 04 PM 7530 setupact.log \r\n-a---- 9/12/2016 4:35 AM 0 setuperr.log \r\n-a---- 6/26/201 8 12:17 PM 275 WindowsUpdate.log \r\n\r\n\r\n'

到处都是\n和\r。如何让它返回所需的标准格式?我还用.PIPE尝试了subprocess Popen方法,得到了相同的结果。你知道吗


Tags: 方法logoutputgetreturnwindowscheckam
1条回答
网友
1楼 · 发布于 2024-04-24 07:21:36

结果是一个编码的bytestring,您必须首先.decode()它,然后print()它才能看到它的格式。\r\n是字符串中的换行符。你知道吗

process = check_output(["powershell.exe", "Get-Childitem C:\\Windows\\*.log"]);
print(process.decode('utf-8'))

相关问题 更多 >