簡體   English   中英

如果該命令沒有 output ,如何制作 if 語句來打印某些內容?

[英]How would make an if statement to print something if there is no output for the command?

我有 winapps 自定義模塊。 如果該軟件存在,它會打印詳細信息; 否則它什么也不做。

如果找不到軟件,我將如何制作 if 語句來打印某些內容?

這是代碼:

with open('output3.txt', 'w') as f:
    for item in winapps.search_installed('ledger'):
        print(item, end='\n\n', file=f)

我建議將匹配的應用程序放入一個列表中,這樣您就可以在決定如何處理它之前輕松測試它是否為空:

ledger_apps = list(winapps.search_installed('ledger'))
if ledger_apps:
    with open('output3.txt', 'w') as f:
        print(*ledger_apps, sep='\n\n', end='\n\n', file=f)
else:
    print("Software wasn't found.")

暫無
暫無

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

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