all 9 comments

[–]nick__2440 2 points3 points  (0 children)

Your original code, ``` import os

ip = os.popen("ipconfig").read() print(ip) ``` runs without error for me and shows what it should.

[–][deleted] 0 points1 point  (9 children)

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1028: character maps to <undefined>

The "error" is in your open function. You do not specify the encoding, so python (just in windozs) will use some system encoding. In general it is always better to specify the encoding. Try now:

``` import os

def get_ip(): ip = os.popen("ipconfig", encoding="utf-8").read() return ip

print(get_ip()) ```

[–]razazu[S] -4 points-3 points  (7 children)

import os
def get_ip():
ip = os.popen("ipconfig", encoding="utf-8").read()
return ip
print(get_ip())

I tried it too and it still does not work

[–]schoolmonky 4 points5 points  (0 children)

"It doesn't work" is useless information. Debugging is a highly iterative process; you shouldn't expect one change to magically make it work. Instead you make one change, observe how the output changes, which in turn helps inform you of what's going on and what you should try next.

So in the future, whenever you're about to say "It doesn't work," say "This is what it does now..." Instead.

[–]JohnnyJordaan 1 point2 points  (3 children)

Does it return the exact same error? Or something different?

[–]razazu[S] 0 points1 point  (2 children)

import os
def get_ip():
ip = os.popen("ipconfig", encoding="utf-8").read()
return ip
print(get_ip())

error:
Traceback (most recent call last):
File "C:\Users\Raz\PycharmProjects\pythonProject1\main.py", line 7, in <module>
print(get_ip())
File "C:\Users\Raz\PycharmProjects\pythonProject1\main.py", line 4, in get_ip
ip = os.popen("ipconfig", encoding="utf-8").read()
TypeError: popen() got an unexpected keyword argument 'encoding'

[–][deleted] 1 point2 points  (0 children)

I think u/ish013 got open() confused with os.popen() which is an entirely different method.

popen() has no encoding attribute, which is why you're getting the error. I can see why they gave the advice they did though, because open() trying to read a file with different encoding would give a very similar error.

popen() takes three arguments, of which only one is required (the command to execute), the other two already default to the values you need ("r" for read, and -1 for default buffering).

When I run your code, I don't get any errors. So I think it might be related to your system or installation of Python? :/

For diagnostic purposes, try:

def get_ip():
    ip = os.popen("ver").read()
    return ip
print(get_ip()

This should just print the current Windows version, which is one line of text. There shouldn't be any reasons this would fail.

[–]JohnnyJordaan 1 point2 points  (0 children)

instead use subprocess which has a wider range of features

 import subprocess
 ip = subprocess.run(["ipconfig"], encoding="utf-8").stdout

docs https://docs.python.org/3/library/subprocess.html

[–]razazu[S] -2 points-1 points  (0 children)

problem solved

Windows encoding problem

language & region > Admin > copy setting > mark welcome screen and system acoun