Day 7–101 Days of DevOps — Python platform module

Prashant Lakhera
2 min readJul 7, 2021

--

Welcome to Day 7 of 101 Days of DevOps. The topic for today is a python platform module.

To view the complete course, please check the below url.

For more info, register via the below link

YouTube Channel link

In Linux, we have a command called uname. This will give you information like Operating System Version, Kernel version, architecture.

$ unameLinux$ uname -r4.14.232-176.381.amzn2.x86_64$ uname -mx86_64$ uname -aLinux ip-172-31-10-219.ec2.internal 4.14.232-176.381.amzn2.x86_64 #1 SMP Wed May 19 00:31:54 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

I was looking for some module on how to achieve a similar thing in Python, and finally, I find Platform Module(a module is a file consisting of Python code).

A platform module is used to access underlying platform data such as operating system, interpreter version information, and hardware.

Now let see some of the functionalities provided by the platform module.

# This is one of the most important functionality that I have used and specifically helpful in case of cross platform scripting. So my use case was if its Linux Platform run specific command and if its window run specific command>>> platform.system()'Linux'# during the early days we use to write script based on architecture if it’s 64bit then perform this build and perform different build in case of 32bit>>> platform.architecture()('64bit', 'ELF')>>> platform.machine()'x86_64'# To get the kernel information>>> platform.uname()uname_result(system='Linux', node='ip-172-31-10-219.ec2.internal', release='4.14.232-176.381.amzn2.x86_64', version='#1 SMP Wed May 19 00:31:54 UTC 2021', machine='x86_64', processor='x86_64')>>> platform.release()'4.14.232-176.381.amzn2.x86_64'# To know the Python version>>> platform.python_version()'3.7.9'

One of the most common use cases of the platform module is writing the cross-platform script and executing the command based on the operating system. In the below case, if the operating system is Linux, execute the ls command, or if it’s window execute the dir command, else print unsupported operating system.

import platformimport osif platform.system() == 'Linux':os.system("ls")elif platform.system() == 'Windows':os.system("dir")elseprint("Unsupported operating system")

Assignment:

  1. Try to explore different use cases of the platform module and try to write a cross-platform script.

I am looking forward to you guys joining the amazing journey.

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet