How to detect Operating System from Grasshopper?

I’m hoping to be able to detect which operating system a Grasshopper definition is running on.

I’ve tried using GHpython and code such as:

import System

print System.OperatingSystem.Platform.Info.Name

But no success yet.

Has anyone else been able to detect the OS from inside Grasshopper?

My use case is to be able to concatenate file paths with folder structures separated by \ for Windows or / for MacOS.

I am not a Python guy but does this help ?
I use Windows

1 Like

You can also use this native Python module:

1 Like

Ah thanks very much to both of you!

I’ve just switched a definition from Windows to MacOS and my importing file paths are broken.

It would be perfect if your first suggestion, Laurent, worked on MacOS like it does in Windows but unfortunately I get an error:

'System.IO' object has no attribute 'DirectorySeparatorChar'

But the platform.system() function suggested by both of you works!

Also Anders thanks for patiently re-answering this question. I did genuine searches for answers and didn’t come across your previous post answering this question.

1 Like

As a follow up question to the community:

Does anyone know why

System.IO.DirectorySeparatorChar

exists on Windows but not on MacOS?

You might want to have a look at the native os.path Python module. It has the os.path.join() function, designed for constructing platform agnostic paths.

1 Like

Woah very interesting!

Thanks Anders.

No worries. Regarding the separator concern, as I recall you can get that using os.path.sep.

Edit: I didn’t fully recall, it’s just os.sep:

https://docs.python.org/3.3/library/os.html?highlight=os.sep#os.sep

2 Likes

Ah thanks for this too. I will test on my Mac when I get home later.
:slight_smile:

Yup I’m using os.sep and it works.

1 Like

Regarding your original question, you can get the OS specific information with the platform module.

import platform

print platform.system()
print platform.release()

# Darwin
# 21.6.0

On macOS, it will output Darwin as system, because that’s the underlying Unix base system. I mean, why not point out the best part of a product? :wink:

1 Like