Python script gets operating system version information

Source: Internet
Author: User
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.

The

script is as follows:

#!/usr/bin/python# encoding:utf-8#-*-Coding:utf8-*-"" "Created by PyCharm.File:LinuxBashShellScriptForOps:getS YstemVersion.pyUser:GuodongCreate date:2016/12/16create time:14:51 "" "Import sysimport osimport Platformim Port subprocessimport codecsimport locale def get_system_encoding (): "" "the encoding of the default system locale but Falls back to the given fallback encoding if the encoding are unsupported by python or could not being determined.  See Tickets #10335 and #5846 "" "try:encoding = Locale.getdefaultlocale () [1] or ' ASCII ' codecs.lookup (encoding) except exception:encoding = ' ASCII ' return encoding default_locale_encoding = get_system_encoding () mswindows = (sys . Platform = = "Win32") # Learning from ' subprocess ' Modulelinux = (Sys.platform = = "Linux2") Hidden_hostname = True if Mswi   Ndows:uname = List (Platform.uname ()) if hidden_hostname:uname[1] = "Hidden_hostname" 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 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, 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

As follows:

(1) Registry information Get location:

(2) Output in the Windows environment:

(3) Output in Linux environment:

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.