Day 7 – Python platform module
Welcome to Day 7 of 101 Days of DevOps. The topic for today is a python platform module.
In Linux, we have a command called uname. This will give you information like Operating System Version, Kernel version, architecture.
$ uname
Linux
$ uname -r
4.14.232-176.381.amzn2.x86_64
$ uname -m
x86_64
$ uname -a
Linux 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 platform
import os
if platform.system() == 'Linux':
os.system("ls")
elif platform.system() == 'Windows':
os.system("dir")
else:
print("Unsupported operating system")
Assignment:
- 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.
- Twitter: @lakhera2015
- Facebook: https://www.facebook.com/groups/795382630808645/
- Medium: https://medium.com/@devopslearning
- GitHub: https://github.com/100daysofdevops/100daysofdevops
- Slack: https://join.slack.com/t/100daysofdevops/shared_invite/zt-au03logz-YfDUp_FJF4rAUeDEbgWmsg
4 Comments
Thank you for doing this sir.
Thanks Subhankar
Hello Lakhera, I’m really enjoying this course. I’ve cached a typo in this lesson, there is a missing colon of the else statement in the script. I’m almost up to date with the course, and I want to thank you for sharing this :), and can’t wait to reach Terraform lessons :). Greeting from Mexico!
Thanks for your input, I have updated the article.