The Python script obtains the operating system version information and the python script version information.
Viewing System Version Information is a common practice. Sometimes you need to enter version information into the asset management system, it is a headache to manually query the information and then input it into the system. If you use a script to complete this task, the situation is different.
In the world of Python, the platform module can be used to obtain Windows Version Information and Linux version information, but the platform Module is not omnipotent, and some special information (such as the Windows internal version number) this module is not available, so we can only find another way.
In Linux, you can simply think that everything is a file. Even if no ready-made command is available, you can use the open () file method to control the file through read/write. Most of Windows information can be found in the registry, so you can get up and down from the registry. Windows registry is a good thing. It is as convenient as everything is a file in Linux. If you want to use Python to access the registry, you need modules apart from permissions, in Python, _ winreg is a built-in module that can be used to read and write the registry.
This script collects some common methods for obtaining version information. In addition to the platform module, there are other modules available for use. Because the platform Module is not a built-in module, additional installation is required. In Windows, permissions and Chinese characters must be taken into account when running scripts. To solve the problem of printing Chinese Characters in Python, use the get_system_encoding () function in the script. This function is taken from Django, after testing, this function is still very useful.
Note: In PyCharm, garbled characters are often displayed in the Run window. Correct transcoding is not performed in the Code, while IDE encoding is also set. If it is developed in Windows, it is recommended that the code be written in UTF-8, IDE encoding is set to "GBK ", setting Method "File" --> "Settings" --> "Editor" --> "File Encoding" --> "IDE Encoding" select "<System Default (now GBK)> ", "Project Encoding" select UTF-8 to ensure code consistency.
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" 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) location for obtaining registry information:
(2) Output in Windows:
(3) Output in Linux: