Python operations-Get various information about the current operating system

Source: Internet
Author: User

#通过Python的psutil模块, get information about the current system (such as memory, CPU, disk, log-in user, etc.) and backup the information # coding=utf-8#  get the basic information of the system import  sysimport psutilimport timeimport os  #获取当前时间time_str  =  time.strftime (   "%y-%m-%d",  time.localtime ( )  ) file_name =  "./"  + time_str +   ". Log" if os.path.exists  ( file_name )  == False :    Os.mknod ( file_name )    handle = open  ( file_name ,  "W"  ) else :   handle = open  ( file_name ,  "A"  ) # Gets the number of command-line arguments If len ( sys.argv )  == 1 :   print_type =  1else :   print_type = 2def isset  ( list_arr , name  )  :    if name in list_arr :        return true    else :       return falseprint_str =  ""; Get system memory Usage if  ( print_type == 1 )  or isset ( SYS.ARGV, "mem"  )    : memory_convent = 1024 * 1024 mem = psutil.virtual_memory ()  print_str +=   "  memory status as follows: \ n"   print_str = print_str +  " The     system has a memory capacity of:  "+str ( mem.total/( memory_convent )  )  + "   mb\n "  print_str = print_str + "     system memory to use capacity:  "+str (  mem.used/( memory_convent )  )  +  " mb\n"   print_str = print_ str +  "    system available memory capacity is: " +str ( mem.total/( memory_convent )  -  mem.used/( 1024*1024 ))  +  "mb\n"  print_str = print_str +  "     B of MemoryUffer capacity is:  "+str ( mem.buffers/( memory_convent )  )  + "  mb\n "   print_str = print_str +  "    Memory Cache Capacity:"  +str ( mem.cached/ ( memory_convent )  )  +  " mb\n" #获取cpu的相关信息if   ( print_type ==  1 )  or isset ( SYS.ARGV, "CPU"  )  : print_str +=  " CPU status as follows: \ n"  cpu_status = psutil.cpu_times ()  print_str = print_str +  "    user =  " + str ( cpu_status.user )  + " \ n "  print_str  = print_str +  "   nice = "  + str ( cpu_status.nice  )  +  "\ n"  print_str = print_str +  "   system =   " + str ( cpu_status.system )  + " \ n " print_str = print_str  +  "   idle =  " + str  ( cpu_status.idle )  + " \ n " print_str  = print_str +  "   iowait = "  + str  ( cpu_ status.iowait )  +  "\ n"  print_str = print_str +  "   irq  =  " + str ( cpu_status.irq )  + " \ n " print_str = print_ str +  "   softirq = "  + str  ( cpu_status.softirq )  +  "\ n"   print_str = print_str +  "   steal =  " + str  ( cpu_status.steal )  + " \ n " print_str = print_str  +  "   guest = "  + str  ( cpu_status.guest )  +   "\ n" #查看硬盘基本信息if   ( print_type == 1 )  or isset  ( SYS.ARGV, " Disk " )  :&NBsp;print_str +=   "  HDD information as follows: \ n"   disk_status = psutil.disk_partitions ()  for item in disk_status :     print_str = print_ str +  "   " + str ( item )  +  "\ n" #查看当前登录的用户信息if   (  print_type == 1 )  or isset  ( sys.argv, "user"  )  : print_str  +=   "  Login user information as follows:\n "   user_status = psutil.users ()  for  item in  user_status :     print_str = print_str +   "   " + str ( item )  +  "\ n" print_str +=  "--------------- ------------------------------------------------\ n "print  ( print_str ) handle.write ( print_ str ) Handle.close ()


This article is from the "codefly" blog, make sure to keep this source http://codefly.blog.51cto.com/8530901/1828837

Python operations-Get various information about the current operating system

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: info-contact@alibabacloud.com 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.