Decode accents at subprocess shell output

Asked

Viewed 26 times

1

I’m executing the command say through the Python subprocess but it returns me the output of the command without the accents.

Code

import subprocess

response = subprocess.check_output('dir', shell=True)
response = (str(response).replace('\\r','')).split('\\n')

for e in response:
    print(e)

Exit

b' O volume na unidade C n\xc6o tem nome.
 O N\xa3mero de S\x82rie do Volume \x82 8A39-7137

 Pasta de C:\\Users\\Loseys\\PycharmProjects\\Testes

02/01/2021  03:39    <DIR>          .
02/01/2021  03:39    <DIR>          ..
16/12/2020  11:44    <DIR>          .idea
25/12/2020  20:42             1.609 Backup_server.py
22/12/2020  11:16             4.777 C.py
25/12/2020  20:31             1.058 Chat.py
25/12/2020  20:14             1.504 Client.py
23/12/2020  09:39               234 client_port_80.py
18/12/2020  12:15             1.254 S.py
19/12/2020  01:54                 0 send.py
25/12/2020  14:53             1.408 server-positronx.py
24/12/2020  13:43               390 server_port_80.py
02/01/2021  03:39               171 simple.py
25/12/2020  21:24             2.290 temp.py
26/12/2020  13:12             3.032 X.py
26/12/2020  00:15               459 Y.py
              13 arquivo(s)         18.186 bytes
               3 pasta(s)   1.347.938.693.120 bytes dispon\xa1veis
'

Decode

I tried to decode the output of the variable Answer in Latin-1 but the accents did not come out correctly, the output remained this way:

The volume in unit C has no name. The N£mero of S Rie of Volume 8A39-7137

Another attempt was with utf-8, which presented error in decoding byte 0xc6:

Unicodedecodeerror: 'utf-8' codec can’t Decode byte 0xc6 in position 24: invalid continuation byte

Doubt

How can I make the accents of the output correctly using the subprocess?

  • Try: response = subprocess.check_output('dir', shell=True).decode(), adds the decode() at the end of that line

  • No, it returns me the same two error outputs that I presented above...

  • Tries with decode(erros='ignore') and see if it is satisfactory, but anyway the best way to list the files of a directory is https://stackoverflow.com/q/3207219/3162303

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.