Why do i get a 0 at the end of a os.system call?

This is the place for queries that don't fit in any of the other categories.

Why do i get a 0 at the end of a os.system call?

Postby manzoor.ahamed » Sat Jul 20, 2013 1:53 pm

>>> os.system("df -h")
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 231G 89G 131G 41% /
udev 3.9G 4.0K 3.9G 1% /dev
tmpfs 1.6G 884K 1.6G 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 3.9G 2.9M 3.9G 1% /run/shm
I am new to python, When i run os.system("df -h") in python as shown above why i am getting 0 at the end of the output how can i get rid of that 0. Any solution will be much helpful

Last edited by Yoriz on Sat Jul 20, 2013 2:21 pm, edited 1 time in total.
Reason: changed the tittle
Posts: 1
Joined: Sat Jul 20, 2013 1:40 pm

Re: Why do i get a 0 at the end of a os.system call?

Postby Yoriz » Sat Jul 20, 2013 2:30 pm

As discused in irc the 0 is the exit status, The subprocess module is preferable to using this function.
Info found in this link http://ubuntuforums.org/showthread.php?t=1254582
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython, some Python 3.3
User avatar
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Why do i get a 0 at the end of a os.system call?

Postby metulburr » Sat Jul 20, 2013 2:47 pm

the interpreter is going to return the return status of the function, whereas if you run it from the terminal, the return status is not used
Code: Select all
metulburr@ubuntu:~$ cat test.py
import os
os.system('df -h')
metulburr@ubuntu:~$ python3 test.py
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda6        99G  7.2G   87G   8% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            3.9G  4.0K  3.9G   1% /dev
tmpfs           797M 1008K  796M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            3.9G  2.7M  3.9G   1% /run/shm
none            100M   52K  100M   1% /run/user
/dev/sda7       443G   33G  388G   8% /home
/dev/sdc1        15G  3.3G   12G  23% /media/metulburr/sd card
/dev/sdg1       2.8T  2.7T  100G  97% /media/metulburr/3TB Hard Drive
/dev/sda2       360G  245G  115G  69% /media/metulburr/WIN7

as already stated, subprocess is preferred over os.system. Also place your code in code tags next time
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
User avatar
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests