Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I created a simple script to locate a user on the local machine. Despite entering any characters in the input box, the answer remains the same. I am grateful for any assistance.

#!/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..."))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is because in the concatination, it should be a space that separates the database and the key:

#!/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..."))

also, the double parentheses are uneccessary.

#!/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...")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading