5

im not new in Python but I have one question left: How do I get the name of the Computer that im on/Device ? I tried many things, this is the last thing, I was in the os Module and found something but if i start the script, it gives me an error that in the os module isn't a 'getDeviceName' function.

from os import *

os.getDeviceName()
  • 1
    check this SO post https://stackoverflow.com/questions/799767/getting-name-of-windows-computer-running-python-script – Max Sep 09 '17 at 05:59

3 Answers3

9
import socket
socket.gethostname()

would also work just fine.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Tim
  • 91
  • 2
  • 7
5

to get the machine name, see platform.node(): https://docs.python.org/3/library/platform.html#platform.node

example:

import platform
print(platform.node())
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • If performance is of any consideration, this should be the top answer!: ```% python -m timeit "import platform;platform.node()" 1000000 loops, best of 5: 238 nsec per loop ``` vs. ```% python -m timeit "import socket;socket.gethostname()" 50000 loops, best of 5: 6.93 usec per loop ``` – sgraaf Jun 20 '22 at 09:45
0

CMD:

echo %USERNAME% > ["drive"]:\["path"]\["filename"].["extension"]

example:

echo %USERNAME% > C:\username.uf

python:


path = open("C:\\username.uf", 'r')

username = path.readlines()

print(username)

Console:

['"your username will display here" /n']

if u don't want this '/n' then u can replace it... i know this will take some extra time but for me this is good cuz ima begginer but i learned some other things to print it just by using python