how to write an if-statement in python that incorporates platform.platform

Question:

i am trying to write a program that prints different things depending on the OS and i’m wanting to write an if-statement to do that.I’m new to python but after looking online for a bit i haven’t been able to find any solution

import platform

print('platform:', platform.platform())

if platform.platform == mac0S:
    print('this is a mac')

Asked By: rainydeer

||

Answers:

I’ll be completing the previous answers and giving some examples.

So first of all you’re going to want to check the platform Documentation

There you’ll find that platform.platform is a function that returns a single string, but it includes the MacOS version, which doesn’t suit your example, so we’ll be using platform.system instead: It returns Windows, Linux or Darwin (for OSX).

import platform

print(platform.system())

if platform.system() == "Darwin":
    print("This is a Mac")
elif platform.system() == "Windows":
    print("This is a Windows")
elif platform.system() == "Linux":
    print("This is a lean, mean, Linux machine")

Hope this helps!

Answered By: scr
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.