Python Forum

Full Version: Os command output in variable shows wrong value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ?
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)
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).