Use Python scripts to get System version information for Windows and Linux

Source: Internet
Author: User
Tags locale

Viewing the system version information is a common thing, sometimes need to input the version information into the asset management system, if each time manually to query the information and then enter the system so it is a headache, if the script to complete the matter, then the situation is Different.

In the Python world, access to the Windows version information and Linux version information can be used platform module, but the platform module is not omnipotent, some special information (such as the Windows build number) This module is not available, Then you can only go the other ways.

In a Linux system, you can simply think that everything is a file, then even if there is no ready-made commands available, you can use the open () file method through the read and write control of the File. Most of the information in Windows can be found in the registry, so you can start with the Registry. The Windows registry is a good thing to take data like under Linux Everything is a file as convenient, if you want to use Python to access the registry, in addition to the permissions are required modules, in Python _winreg is a built-in module, through which the registry can read and Write.

This script collects some common ways to get version information, in addition to the platform module, there are other modules to use, because the platform module is not a built-in module, so additional installation is Required. Windows run scripts need to consider the problem of permissions and Chinese characters, The problem of solving Python print characters is implemented by the Get_system_encoding () function in the script, which is taken from django, which is very useful after testing this Function.

Note: in pycharm, often encountered in the run window printed in the Chinese display garbled, the code is not properly transcoded is one hand, and the Ide's encoding settings is also an aspect. If it is developed under windows, then the recommended code is written in UTF-8, and the Ide's encoding is set to "GBK", the method "file"--"Settings"--"editor" and "file Encoding"-- > IDE Encoding Select <system Default (now GBK) >, Project Encoding Select UTF-8 to guarantee code CONSISTENCY.

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " border= "0" alt= "image" src= "http://s3.51cto.com/wyfs02/M02/8B/A7/wKiom1hTq8qj_iAsAANfbzAONsk729.png" height= "558" />

The script is as Follows:

#!/usr/bin/python# encoding: utf-8# -*- coding: utf8 -*-"" "Created by  pycharm.file:                linuxbashshellscriptforops:getsystemversion.pyuser:                GuodongCreate Date:         2016/12/16create time:        14:51  "" "import sysimport  osimport platformimport subprocessimport codecsimport localedef get_system_ Encoding ():     "" "    the encoding of the default  system locale but falls back to the given     Fallback encoding if the encoding is unsupported by python or  could    not be determined.  see tickets  #10335  and  #5846      "" "    try:        encoding =  Locale.getdefaultlocale () [1] or  ' ASCII '         codecs.lookup (encoding)     except Exception:         encoding =  ' ASCII '     return encodingDEFAULT_LOCALE_ENCODING =  Get_system_encoding () mswindows =  (sys.platform ==  "win32")   # learning  from  ' subprocess '  modulelinux =  (sys.platform ==  "linux2") hidden_hostname  = trueif mswindows:    uname = list (platform.uname ())      if hidden_hostname:        uname[1] =   "hidden_hostname" &NBSP;&NBSP;&NBSP;&NBsp;print uname    import _winreg    try:         reg_key = _winreg. Openkey (_winreg. hkey_local_machine,  "software\\microsoft\\windows nt\\currentversion")          if reg_key:             Productname = _winreg. QueryValueEx (reg_key,  "ProductName") [0] or none             editionid = _winreg. QueryValueEx (reg_key,  "editionid") [0] or none             releaseid = _winreg. QueryValueEx (reg_key,  "releaseid") [0] or none             currentbuild = _winreg. QueryValueEx (reg_key,  "currentbuild") [0] or none  &nbsP;         buildlabex = _winreg. QueryValueEx (reg_key,  "buildlabex") [0][:9] or none             print  (productname, editionid, releaseid, currentbuild,  buildlabex)     except Exception as e:         print e.message.decode (default_locale_encoding) if linux:     Uname = list (platform.uname ())     if hidden_hostname:         uname[1] =  "hidden_hostname"     print uname     proc_obj = subprocess. Popen (r ' uname -a ',  shell=true, stdout=subprocess. pipe,                         &nBsp;       stderr=subprocess. STDOUT)     result = proc_obj.stdout.read (). strip (). decode (default_locale_ ENCODING)     if result:        print  Result    if os.path.isfile ("/proc/version"):         with open ("/proc/version",  ' r ')  as f:             content = f.read (). Strip ()          if content !=  "":             print content    if os.path.isfile ("/etc/issue"):         with open ("/etc/issue",  ' r ')  as f:             content = f.read (). Strip ()         if content !=  "":             print content

note: The script content can be obtained via github, address: https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/functions/system/getSystemVersion.py :.

As follows:

(1) registry information Get Location:

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " border= "0" alt= "image" src= "http://s3.51cto.com/wyfs02/M01/8B/A7/wKiom1hTq8zBxU_qAAOKQjWLr1o654.png" height= "618" />

(2) output in the Windows Environment:

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " border= "0" alt= "image" src= "http://s3.51cto.com/wyfs02/M02/8B/A3/wKioL1hTq82h7zDzAABR_tXHjF8283.png" height= "393" />

(3) output in Linux environment:

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " border= "0" alt= "image" src= "http://s3.51cto.com/wyfs02/M02/8B/A7/wKiom1hTq82hj5qjAABURhBNFbA898.png" height= "229" />

Tag:python system version, Windows build, Python Operation Registry

--end--

This article is from "communication, my favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1883428

Use Python scripts to get System version information for Windows and Linux

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.