簡體   English   中英

Python 子進程的 If else 語句似乎不起作用

[英]The If else statement for Python's subprocess doesn't seem to work

我創建了一個簡單的腳本來定位本地機器上的用戶。 盡管在輸入框中輸入了任何字符,但答案保持不變。 我很感激任何幫助。

#!/usr/bin/python

import subprocess

user = input("Enter username : ")

result = subprocess.getoutput("getent passwd" + user)
if result:
 print(("found "+user+" user in this system."))
else:
 print((""+user+" is not found..."))

這是因為在連接中,它應該是一個分隔databasekey的空間:

#!/usr/bin/python
import subprocess

user = input("Enter username : ")

result = subprocess.getoutput("getent passwd " + user) #added space after 'passwd'
if result:
 print(("found "+user+" user in this system."))
else:
 print((""+user+" is not found..."))

此外,雙括號是不必要的。

#!/usr/bin/python
import subprocess

user = input("Enter username : ")

result = subprocess.getoutput("getent passwd " + user) #added space after 'passwd'
if result:
 print("found "+user+" user in this system.")
else:
 print(user+" is not found...")

暫無
暫無

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

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