代碼:os.popen()閱讀()掛起,直到我在控制檯中鍵入「exit」
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 |找到「原始安裝日期」')。read() 但是不能確定。
我使用Python 3.5
」但不能確定。「爲什麼不?放一堆打印語句,找出它掛起的地方。 –
我試過這個......它沒有執行打印語句。 –