A first programme

I’m now going to bring some concepts together from all the previous notes.

This functionality might be useful in any script and will allow you to distinguish the platform the script is running on. You can then include any platform specific functions in the correct if/elif section.

Save this program as osdetect.py, run it and make sure you understand every line!

import platform 

def detect_os()->str:
    # Detect the OS in use
    return platform.system()


if (__name__ == '__main__'):
    print(f"This module is called {__name__} and executes as a standalone script")
    
    # Check the OS in use, lower case
    my_os = detect_os()
    my_os = my_os.lower()
    
    # Parse the response
    if my_os == "windows":
        print("Your system is Windows")
    elif my_os == "linux":
        print("Your system is Linux")
    elif my_os == "darwin":
        print("Your Apple system is MacOS")
    elif my_os == "cygwin":
        print("Your Apple system is MacOS")
    elif my_os == "aix":
        print("Your IBM system is AIX")
    else:
        print(f"Unidentified system = {my_os}")
else:
    print(f"This module is called {__name__} and is being called by another script")