Python script gets the operating system version information _python

Source: Internet
Author: User
Tags locale python script in python

Viewing system version information is a commonplace thing, sometimes need to input version information into the asset management system, if each manual to query the information and input system is a headache, if the script to complete this matter, then the situation is different.

In the Python world, getting Windows version information and Linux version information can take the platform module, but the platform module is not omnipotent, some special information (such as Windows version number) This module is not available, Then there's only a choice.

In a Linux system, you can simply think of everything as a file, so you can use the open () file to control the file's reading and writing, even if no ready-made commands are available. Most of the information in Windows can be found in the registry, so you can start from the registry. Windows registry is a good thing, take the data just like in Linux everything is as convenient as files, if you want to access the registry with Python, in addition to the permissions is required module, in Python _winreg is a built-in module, through this module can read and write to the registry.

This script collects some common ways to get version information, in addition to the Platform module, there are other modules available because the Platform module is not a built-in module and therefore requires additional installation. Running a script under Windows requires permission issues and Chinese character issues, and the problem with writing in Python printing is done through the get_system_encoding () function in the script, which is taken from Django, and is very useful for testing this function.

Note: In the pycharm, often encountered in the run window printed in Chinese display garbled, the code does not pass the correct transcoding is on the one hand, and the IDE's coding settings is also an aspect. If you are developing under Windows, then the recommended code is written in UTF-8, the IDE's encoding is set to "GBK", set method "file"--> "Settings"--> "Editor"--> "File Encoding"- > IDE Encoding Select <system Default (now GBK) >, Project Encoding choose UTF-8 to guarantee code consistency.

The

script is as follows:

#!/usr/bin/python # encoding:utf-8 #-*-Coding:utf8-*-"" "Created by Pycharm.    File:LinuxBashShellScriptForOps:getSystemVersion.py User:guodong Create date:2016/12/16 Create Time: 14:51 "" Import sys import OS import platform import subprocess import codecs import locale Def get_system_encoding ( ): "" "" "" encoding of the default system locale but falls back to the given fallback encoding if the encoding is UN Supported by Python or could is determined. Tickets #10335 and #5846 "" "try:encoding = Locale.getdefaultlocale () [1] or ' ASCII ' Codecs.lookup (Encodin g) Except exception:encoding = ' ASCII ' return encoding default_locale_encoding = get_system_encoding () mswindo WS = (Sys.platform = "Win32") # Learning from ' subprocess ' module Linux = (sys.platform = "linux2") Hidden_hostname = T

  Rue if 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, Buildlabe x) 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:con
 Tent = F.read (). Strip () If content!= "": Print Content

Screenshot below:

(1) Registry information Get location:

(2) Output in Windows environment:

(3) The output of 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.