Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return system information in Python?

Tags:

Using Python, how can information such as CPU usage, memory usage (free, used, etc), process count, etc be returned in a generic manner so that the same code can be run on Linux, Windows, BSD, etc?

Alternatively, how could this information be returned on all the above systems with the code specific to that OS being run only if that OS is indeed the operating environment?

like image 891
davidmytton Avatar asked Jan 21 '09 19:01

davidmytton


People also ask

What does OS system () return?

os. system only returns the error value.


1 Answers

Regarding cross-platform: your best bet is probably to write platform-specific code, and then import it conditionally. e.g.

import sys if sys.platform == 'win32':   import win32_sysinfo as sysinfo elif sys.platform == 'darwin':   import mac_sysinfo as sysinfo elif 'linux' in sys.platform:   import linux_sysinfo as sysinfo #etc  print 'Memory available:', sysinfo.memory_available() 

For specific resources, as Anthony points out you can access /proc under linux. For Windows, you could have a poke around at the Microsoft Script Repository. I'm not sure where to get that kind of information on Macs, but I can think of a great website where you could ask :-)

like image 151
John Fouhy Avatar answered Oct 13 '22 00:10

John Fouhy