Python Forum
how to do comparison for the value returned by os.system
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to do comparison for the value returned by os.system
#1
In my code, I’m trying to compare the output returned by the os.system(’/opt/abc/cmd/abc-target’)

Here’s my code:

import os
s= os.system(’/opt/abc/cmd/abc-target’)
if (s == ‘abc’ ):
print (‘great you found the target system’)

The above code doesn’t throw any error but doesn’t execute the if statement. I want the comparison to match and print success.
Please help me understand what I'm doing wrong.
Reply
#2
This is how os.system() work.
You get only a number back (0==success (else error)).
See https://docs.python.org/3/library/os.html#os.system
Reply
#3
Avoid os.system() by all means(security reasons),and use subprocess instead:
Quote:os.system() and os.popen*() have been deprecated since Python 2.6 in favor of the subprocess module. 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to access values returned from inquirer cspower 6 695 Dec-26-2023, 09:34 PM
Last Post: cspower
  SQLAlchemy Object Missing when Null is returned Personne 1 1,675 Feb-19-2022, 02:50 AM
Last Post: Larz60+
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,528 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Libraries installed with pipenv, but ModuleNotFoundError returned jpncsu 2 2,945 Sep-06-2021, 07:24 PM
Last Post: jpncsu
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,491 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Exception: Returned Type Mismatch Error devansing 1 5,088 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  How to use a returned value? t4keheart 12 4,750 Jan-16-2020, 06:54 AM
Last Post: perfringo
  Strange Characters in JSON returned string fioranosnake 4 5,117 Dec-02-2019, 07:25 PM
Last Post: fioranosnake
Question Difference between Python's os.system and Perl's system command Agile741 13 6,648 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Not all data returned from sys.argv ecdhyne 2 2,723 Sep-05-2019, 08:27 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020