簡體   English   中英

Python程序不會將所有信息都寫入.txt,但是會寫入外殼

[英]Python program won't write all information to .txt but it will to the shell

Python程序不會將所有信息都寫入.txt,但是會寫入外殼

from time import sleep
import platform, os, sys
log = open("reportLog.txt", "w") 
log.write("####REPORT####\n")


try:
    sys =("SYS: " + platform.system()+ ("\n"))
    syslog = log.write(sys)
    syslog()
except:
    pass
try:
    os = ("OS: " + platform.os()+ ("\n"))
    oslog = log.write(os)
    oslog()

except:
    pass
try:
    platform = ("PLATFORM: "+ platform.platform()+ ("\n"))
    platoformlog = log.write(platform)
    platofrmlog()
except:
    pass
try:
    mac_ver("MAC_VER: " + platform.mac_ver()+ ("\n"))
    mac_verlog = log.write(mac_ver)
    mac_verlog()
except:
    pass
try:
    dist =("DIST: " + platform.dist()+ ("\n"))
    distlog = log.write(dist)
    distlog()
except:
    pass
try:
    node = ("NODE: " + platform.node()+ ("\n"))
    nodelog = log.write(node)
    nodelog()
except:
    pass
try:
    arch =("ARCHITECTURE: " + platform.architecture()+ ("\n"))
    archlog = log.write(arch)
    archlog()
except:
    pass
try:
    machine =("MACHINE: " + platform.machine() + ("\n"))
    machinelog = log.write(machine)
    machinelog()
except:
    pass
try:
    cpu =("CPU: " + platform.processor() + ("\n"))
    cpulog = log.write(cpu)
    cpulog()

except:
     pass
log.write("##########")
log.close()

您的代碼存在以下問題:

  1. 你不必要地重復自己
  2. except: pass總是錯誤的
  3. platform.os不是函數類型

簡明正確的代碼版本是

import platform

with open("reportLog.txt", "w") as log:
    log.write("####REPORT####\n")

    names =  """dist system platform mac_ver dist node architecture 
        machine processor""".split()

    for name in names:
        label = name.upper()
        getter = getattr(platform, name)
        value = getter()

        log.write('{}: {}\n'.format(label, value))

    log.write("##########")

我列出了要調用的平台中的函數名稱,然后迭代這些名稱。 我已經從名稱中刪除了“ os”,因為plaftorm.os只是對模塊os的引用,並且我認為它已被system覆蓋

對於每個名稱,我通過大寫來生成標簽。 對於每個名稱,我從平台模塊中獲取具有該名稱的函數。 要獲取調用該函數的返回值,請調用它。 我使用更常見的方式將格式化的字符串組合在一起並編寫它。 因為打開文件是用上下文管理器包裝的,所以當上下文管理器結束時,它將為我關閉文件。

我沒有例外,因為我什么也沒期待。 如果有任何例外,我的代碼中有一個錯誤,我想盡可能地了解它。 未捕獲的異常最終將使程序崩潰,並打印出不錯的回溯記錄,這樣我就可以知道我在哪里搞砸了。 默默地忽略例外情況就像是在哭泣的嬰兒的嘴巴和鼻子上用膠帶扎起來一樣,暫時使事情安靜下來,只會在將來造成更嚴重的問題(死去的嬰兒)。

不要將信息存儲在元組中,而應使用普通字符串。 例如:

sys = "SYS: " + platform.system()+ "\n"
syslog = log.write(sys)

更好的是,您可以使用format函數獲得更簡潔的代碼:

sys = "SYS: {0}\n".format(platform.system())
syslog = log.write(sys)

另外,請確保捕獲異常以了解問題原因:

try:
    sys = "SYS: " + platform.system()+ "\n"
    syslog = log.write(sys)
except Exception as e:
    print(e)

除了msw指出的問題外,我不明白您為什么要執行以下操作:

syslog = log.write(sys)
syslog()

log.write不返回任何值( log.writeNone值)。 但是,您可以將None分配給變量,並像執行函數一樣執行該變量。

我的解決方案比msw的解決方案更簡單,但代價是更長:

with open("reportLog.txt", "w") as log:
    log.write("####REPORT####\n")
    log.write('SYS: {}\n'.format(platform.system()))
    # Other log.write lines here...
    log.write("##########")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM