【问题标题】:Python subprocess returns with bad formatting when used with powershell与 powershell 一起使用时,Python 子进程返回格式错误
【发布时间】:2018-06-26 20:53:44
【问题描述】:

我一直在尝试以下方法:

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

它返回一个输出:

b'\r\n\r\n 目录:C:\Windows\r\n\r\n\r\nMode LastWriteTime Leng 名称 \r\n---- ------------- - ----- ---- \r\n-a---- 2018 年 6 月 14 日上午 11:26 4056 DtcInstall.log \r\n-a---- 2018 年 6 月 21 日上午 11:33 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 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 的子进程 Popen 方法,它给出了相同的结果。

【问题讨论】:

标签: python powershell subprocess


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-03-14
    • 2020-12-08
    • 2019-02-27
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    相关资源
    最近更新 更多