Python Forum
Os command output in variable shows wrong value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Os command output in variable shows wrong value
#1
I have this :

v_num_distinct_phy_disks_in_cells=os.system('cat /tmp/p_count_num_phy_disks_all_cell | sort -u | wc -l')
The content of file is :
cat /tmp/p_count_num_phy_disks_all_cell
12
12
12
If I run manually it shows correct as value of distinct number is 1:
cat /tmp/p_count_num_phy_disks_all_cell | sort -u | wc -l
1
If I run in Python I get this:
>>> v_num_distinct_phy_disks_in_cells=os.system('cat /tmp/p_count_num_phy_disks_all_cell | sort -u | wc -l')
1
>>> print(v_num_distinct_phy_disks_in_cells)
-1
Why it is showing -1 ?
Reply
#2
I found a solution!

v_num_distinct_phy_disks_in_cells = subprocess.check_output("cat /tmp/p_count_num_phy_disks_all_cell | sort -u | wc -l", shell=True);
print (v_num_distinct_phy_disks_in_cells)
Reply
#3
Per the documentation, the return value of os.system is the return value of the process, not what it prints to standard out (or anything else).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 996 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 3,247 Mar-27-2023, 07:38 AM
Last Post: buran
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 1,855 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 2,147 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 5,173 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  How to combine two output in one variable? ilknurg 2 1,969 Aug-01-2022, 09:31 AM
Last Post: Gribouillis
  How to write a part of powershell command as a variable? ilknurg 2 1,925 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  ModuleNotFound but pip shows module installed biscotty666 2 2,640 Jul-14-2022, 05:17 PM
Last Post: Axel_Erfurt
  How to use a variable in linux command in python code? ilknurg 2 2,431 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Command output to Variable ironclaw 1 3,867 Aug-26-2021, 06:55 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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