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 -r
$ uname -m
$ 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
# 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
# To get the kernel information
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')
# To know the Python version
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.
if platform.system() == 'Linux':
elif platform.system() == 'Windows':
print("Unsupported operating system")
- 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