【发布时间】:2017-06-12 22:16:46
【问题描述】:
我有一个 Python 3.6.0 脚本,我在系统上运行 autorunsc v13.71 (https://technet.microsoft.com/en-us/sysinternals/bb963902.aspx)(x86 或 x86_64 版本,根据使用 platform.machine() 的系统位数)。如果我直接从终端(CMD 或 Powershell)运行autorunsc,我会按预期得到输出,没有问题(从输出中截取):
但是,如果我尝试使用我的代码运行它,我会得到这个混乱的输出:
我正在使用 Window 的默认记事本打开输出文本文件。人们应该可以使用记事本阅读它,他们将无法下载 Notepad++、ST3 等代码阅读器。
我的代码(删除了一些部分以使其简短直接):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import platform
import socket
import subprocess
from time import gmtime, strftime
from pathlib import Path
HOSTNAME = socket.gethostname()
SYS_ARCH = platform.machine() # AMD64 or x86
ROOT_PATH = Path(__file__).parent
def get_current_datetime():
return strftime("%Y-%m-%d %H:%M:%S UTC%z", gmtime())
def run_command(output_file, command_name, command_args, system_cmd=False):
output_file.write(f'---------- START [{command_name} {SYS_ARCH}] {get_current_datetime()} ----------\n')
output_file.flush()
file_path = command_name if system_cmd else str(ROOT_PATH / 'tools' / SYS_ARCH / (command_name + '.exe'))
subprocess.call([file_path] + command_args, stdout=output_file, shell=True, universal_newlines=True)
output_file.write(f'---------- ENDED [{command_name} {SYS_ARCH}] {get_current_datetime()} ----------\n\n')
output_file.flush()
print(f'[*] [{command_name} {SYS_ARCH}] done')
def main():
output_file = ROOT_PATH.parent / (HOSTNAME + '.txt')
with open(output_file, 'w', encoding='utf-8') as fout:
run_command(output_file=fout, command_name='autorunsc', command_args=['-h', '-nobanner', '-accepteula'])
if __name__ == '__main__':
main()
文件结构:
- 文件夹\
- app.py(此处显示的代码)
- 工具\
- AMD64\
- autorunsc.exe
- x86\
- autorunsc.exe
- AMD64\
我相信这与 autorunsc 的输出有关,我在某处读到它返回编码为 UTF-16 的输出。问题是我运行了许多其他 Sysinternals EXE 并将输出附加到同一个文件(使用我的 run_command 函数),并且所有这些都可以完美地工作,但是这个。我怎样才能做到这一点?
【问题讨论】:
标签: python sysinternals