Categories
Python Answers

How to check which OS a script is running on with Python?

Spread the love

Sometimes, we want to check which OS a script is running on with Python.

In this article, we’ll look at how to check which OS a script is running on with Python.

How to check which OS a script is running on with Python?

To check which OS a script is running on with Python, we can use the os.name property or platform.system method.

For instance, we write:

import os

print(os.name)

And we see 'posix' printed when we run the script in a Unix like OS.

We can get more specific OS info with platform.system and platform.release.

For instance, we write:

import platform

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

We call platform.system to get the name of the OS the script is currently running on.

And we call platform.release to get the release info of the OS the script is currently running on.

Therefore, we get something like:

Linux
5.11.0-1021-gcp

printed.

Conclusion

To check which OS a script is running on with Python, we can use the os.name property or platform.system method.

We call platform.system to get the name of the OS the script is currently running on.

And we call platform.release to get the release info of the OS the script is currently running on.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *