get system name in python

You can use the platform library in Python to get the name of the operating system:

main.py
import platform

system_name = platform.system()

print("System Name:", system_name)
85 chars
6 lines

This will output the name of the operating system that Python is running on, for example:

main.py
System Name: Windows
21 chars
2 lines

You can also use the os library to get the hostname or computer name of the system:

main.py
import os

computer_name = os.getenv('COMPUTERNAME')

print("Computer Name:", computer_name)
93 chars
6 lines

This will output the name of the computer, for example:

main.py
Computer Name: MY-COMPUTER
27 chars
2 lines

Note that this will only work on Windows operating systems. On other operating systems, you can use the socket library to get the hostname:

main.py
import socket

hostname = socket.gethostname()

print("Hostname:", hostname)
77 chars
6 lines

This will output the hostname of the system, for example:

main.py
Hostname: my-laptop
20 chars
2 lines

gistlibby LogSnag