os.popen()。read()挂起,直到我在控制台中输入“exit”

时间:2017-08-07 18:28:08

标签: python-3.x

代码:

import platform
import socket
import webbrowser
import psutil
from subprocess import check_output
import re
import urllib.request
import winreg as reg
from datetime import datetime
import os
import time
import sys
import subprocess

outfile = open('platform.txt', "w")

hardwareList = ["Platform: " + platform.system()]

hardwareList.append("Operating System: " + platform.platform())

info = os.popen('cmd /k systeminfo | find "Original Install Date"').read()
stripInfo = info.replace('Original Install Date:     ','')
hardwareList.append("OS Installation Date: " + stripInfo.strip())


hardwareList.append("Processor: " + platform.processor())

hardwareList.append("Qty Physical CPU's: %s " % psutil.cpu_count(logical=False))

hardwareList.append("Qty Logical CPU's: %s" % psutil.cpu_count(logical=True))

hardwareList.append("Qty Cores: %s" % (psutil.cpu_count(logical=True)/psutil.cpu_count(logical=False)))

memory = psutil.virtual_memory()
hardwareList.append("RAM: " + str("{:3.2f}".format(memory[0]/1024/1024/1024)))

diskUsage = psutil.disk_usage('C:/')
total = "{:4.2f}".format((diskUsage[0]/1024/1024/1024))
avail = "{:4.2f}".format((diskUsage[2]/1024/1024/1024))
used = "{:4.2f}".format((diskUsage[1]/1024/1024/1024))

hardwareList.append("Total Capacity: %s " % total + "MB")

hardwareList.append("Total Free: %s " % avail + "MB")

hardwareList.append("Total Used: %s " % used + "MB")

clocksp = str(check_output("wmic cpu get MaxClockSpeed"))
clocksp1 = re.findall(r'\d+',clocksp)
clocksp2 = (float(clocksp1[0])/1000)
hardwareList.append("Clockspeed: %s" % clocksp2 + " GHz")

hardwareList.append("Network Data:")

hardwareList.append("HostName: " + socket.gethostname())

hardwareList.append("FQDN: " + socket.getfqdn())

hardwareList.append("IP Address: " + socket.gethostbyname(socket.gethostname()))

dbTypes = ['MongoDB','MSSQLSERVER','MySQL','postgresql-x64-9.5']
for db in dbTypes:
    try:
        dbInfo = psutil.win_service_get(db).as_dict()        
        hardwareList.append("Database Name: " + dbInfo['display_name'] + ", status=" + dbInfo['status'])
    except:
        search = dbInfo['display_name'].find(db)
        if search != -1:
            hardwareList.append(search)
        else:
            hardwareList.append("Database Name: " + db + ", status=Not installed")

softwareList = []
for dir in os.listdir("C:\Program Files"):
    if os.path.isdir("C:\Program Files"+"\\"+dir):        
        hardwareList.append("Software: " + dir)

for ins in hardwareList:
    outfile.write(str(ins) + "\r\n")

outfile.close()

所以这里发生的是代码挂起并且无法完成(它生成一个没有数据的空白文本文件,名为'platform.txt'),直到我在控制台中输入'exit'。我想让它在没有人为干预的情况下运行,我不知道python是否足以弄清楚如何克服这个问题。 帮助我让它运行而不必在控制台中输入“exit”。

注意:我已经尝试过子进程,但是在所有难以阅读的手册文档中都丢失了,而StackOverflow上提供的其他解决方案并没有按照我的意愿去做。 我相信它在这条线上失败了:     info = os.popen('cmd / k systeminfo | find“原始安装日期”')。read() 但不能确定。

我正在使用Python 3.5

1 个答案:

答案 0 :(得分:2)

/k的{​​{1}}选项意味着"执行字符串指定的命令,但仍然是" - 换句话说,你明确告诉它以这种不受欢迎的方式行事。请尝试cmd - "执行string指定的命令,然后终止"。