Basic python question

Peter Otten __peter__ at web.de
Thu Oct 3 03:40:04 EDT 2019


Jagga Soorma wrote:

> Thanks again Aldwin.  This seems to work, guess it is the set that is
> flipping the numbers:
> 
> x,y = (output.split())

The parens on the right are superfluous:

>>> a, b = "foo bar".split()
>>> a
'foo'
>>> b
'bar'

> inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' '"
> output  = subprocess.check_output( inode_cmd,
> stderr=subprocess.STDOUT, shell=True )

You may also do the post-processing in Python, which allows you to avoid the 
shell completely:

>>> output = subprocess.check_output(["/bin/df", "--output=pcent,ipcent", 
"/var"])
>>> output
b'Use% IUse%\n 85%   16%\n'
>>> a, b = output.replace(b"%", b"").split()[-2:]
>>> a
b'85'
>>> b
b'16'

If you want integers:

>>> a, b = [int(v) for v in output.replace(b"%", b"").split()[-2:]]
>>> a
85
>>> b
16





More information about the Python-list mailing list