How to identify on which OS Python is running on?
>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'
The output of platform.system()
is as follows:
- Linux:
Linux
- Mac:
Darwin
- Windows:
Windows
See: platform
— Access to underlying platform’s identifying data
Detect OS with python
Use the platform
module:
import platform
print(platform.system())
print(platform.release())
print(platform.version())
Note that a system running on Mac will return 'Darwin' for platform.system()
platform.platform()
will return extremely detailed data, such as
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'
Can python detect which OS is it running under?
I usually just use this:
import os
if os.name == 'nt':
pass # Windows
else:
pass # other (unix)
edit:
Hopefully in response to your comments:
from time import strftime
import os
if os.name == 'nt': # Windows
basePath = 'C:\\working\\'
else:
basePath = '/working/'
Fn = '%sSetup%s.csv' % ( basePath, strftime( '%y%m%d' ) )
How do I check the operating system in Python?
You can use sys.platform
:
from sys import platform
if platform == "linux" or platform == "linux2":
# linux
elif platform == "darwin":
# OS X
elif platform == "win32":
# Windows...
sys.platform
has finer granularity than sys.name
.
For the valid values, consult the documentation.
See also the answer to “What OS am I running on?”
How do I check if I'm running on Windows in Python?
Python os module
Specifically for Python 3.6/3.7:
os.name
: The name of the operating
system dependent module imported. The
following names have currently been
registered: 'posix', 'nt', 'java'.
In your case, you want to check for 'nt' as os.name
output:
import os
if os.name == 'nt':
...
There is also a note on os.name
:
See also
sys.platform
has a finer granularity.os.uname()
gives
system-dependent version information.The platform module provides
detailed checks for the system’s identity.
How can I determine if the operating system a Python script is running on is Unix-like?
The Pythonic way to do it is not to care what platform you are on.
If there are multiple different facilities to accomplish something depending on the platform, then abstract them behind a function or class, which should try a facility and move on to another if that facility is not available on the current platform.
How do I detect different OS system
os.name
would return nt
if your system is Windows, and posix
if POSIX, so you can do it like this
import os
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
Check if a process is running or not on Windows?
You can not rely on lock files in Linux or Windows. I would just bite the bullet and iterate through all the running programs. I really do not believe it will be as "expensive" as you think. psutil is an excellent cross-platform python module cable of enumerating all the running programs on a system.
import psutil
"someProgram" in (p.name() for p in psutil.process_iter())
Related Topics
Importerror: Libtk8.6.So: Cannot Open Shared Object File: No Such File or Directory
How to Use Os.Umask() in Python
How to Perform Low Level I/O on a Linux Device File in Python
Python Ctypes Not Loading Dynamic Library on MAC Os X
Why Don't These Apt-Packages Behave the Same on Ubuntu and Heroku
Creating a Cron Job - Linux/Python
Could Not Find or Load the Qt Platform Plugin "Xcb"
Opencv (Via Python) on Linux: Set Frame Width/Height
Why File Read Is Faster on Reading Again
Python Program with Notification in Gnome Shell Doesn't Work
Process Dies, If It Is Run via Paramiko Ssh Session and with "&" in the End
How to Use Tensorflow Without Cuda on Linux
How Can Python Handle Systemctl Stop
Fitting Empirical Distribution to Theoretical Ones With Scipy (Python)