Python obtains system information (system version, CPU information, memory information, and hard disk information)

Source: Internet
Author: User

CodeAs follows:

Import osimport sysimport subprocessdef get_linux_version (): Print ("System Version ---- % s" % ",". join (sys. version. split ("\ n") def get_cpu_info (): processor_cnt = 0 cpu_model = "" f_cpu_info = open ("/proc/cpuinfo") Try: For line in f_cpu_info: if (line. find ("processor") = 0): processor_cnt + = 1 Elif (line. find ("model name") = 0): If (cpu_model = ""): cpu_model = line. split (":") [1]. strip () print ("CPU counts: % s, CPU model: % s" % (processor_cnt, cpu_model) Finally: f_cpu_info.close () def get_mem_info (): mem_info = "" f_mem_info = open ("/proc/meminfo") Try: For line in f_mem_info: If (line. find ("memtotal") = 0): mem_info + = line. strip () + "," Elif (line. find ("swaptotal") = 0): mem_info + = line. strip () Break print ("mem_info ---- {: s }". format (mem_info) Finally: f_mem_info.close () def get_disc_info (): # disc_info = OS. popen ("DF-h "). read () # disc_info = subprocess. popen ("DF-h", shell = true ). communicate () [0] # print (disc_info) pipe = subprocess. popen ("DF-h", stdout = subprocess. pipe, shell = true) disc_info = pipe. stdout. read () print (disc_info)

without comments, the method is simple. The obtained version is the Sys object inside python. The obtained CPU and memory information is directly read the files under/proc, and then parse; to obtain hard disk information, run the shell command and save it to the python variable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.