I am most likely missing some really easy, but I can't wrap my head around why what seems to work for everyone else doesnt work for me.
Goal: I want to run shell commands with native output in non-english characters, capture the output in a variable then print to screen.
Problem: All my output that should have the non-english characters are replaced with ? marks.
Thoughts: is there an encoding issue? I am running python 3.8, shouldnt be!! Also running Windows 10, but also happens in Windows 7 and Server 2008.
>>> p=subprocess.run("dir",shell=True,encoding="utf8")
Volume in drive C has no label.
Volume Serial Number is A22B-FA10
Directory of C:\Users\jeronimo\Documents\Github
04/24/2021 08:17 AM <DIR> .
04/24/2021 08:17 AM <DIR> ..
07/21/2020 09:37 PM <DIR> scripts
04/24/2021 08:09 AM <DIR> **Администратор**
1 File(s) 295 bytes
11 Dir(s) 151,978,950,656 bytes free
>>> p=subprocess.run("dir",capture_output=True,shell=True,encoding="utf8")
>>> p.stdout
' Volume in drive C has no label.\n Volume Serial Number is A22B-FA10\n\n Directory of C:\\Users\\jeronimo\\Documents\\Github\n\n04/24/2021 08:17 AM <DIR> .\n04/24/2021 08:17 AM <DIR>
..\n05/18/2020 01:24 PM scripts\n04/24/2021 08:09 AM <DIR> **?????????????**\n 1 File(s) 295 bytes\n 11 Dir(s) 151,976,796,160 bytes free\n'
>>> print(p.stdout)
Volume in drive C has no label.
Volume Serial Number is A22B-FA10
Directory of C:\Users\jeronimo\Documents\Github
04/24/2021 08:17 AM <DIR> .
04/24/2021 08:17 AM <DIR> ..
07/21/2020 09:37 PM <DIR> scripts
04/24/2021 08:09 AM <DIR> **?????????????**
1 File(s) 295 bytes
11 Dir(s) 151,976,796,160 bytes free
EDIT: I've tried piping out to a file:
>>> f=open('file','a+',encoding='utf-8')
>>> p=subprocess.call("dir",shell=True,encoding="utf8",stdout=f)
>>> f.close()
Volume in drive C has no label.
Volume Serial Number is A22B-FA10
Directory of C:\Users\jeronimo\Documents\Github
04/24/2021 11:49 AM <DIR> .
04/24/2021 11:49 AM <DIR> ..
07/21/2020 09:37 PM <DIR> scripts
04/24/2021 08:09 AM <DIR> ?????????????
1 File(s) 0 bytes
11 Dir(s) 151,974,350,848 bytes free
I've tried many variations of subprocess - popen, run, check_output, call - all give the same result. What the heck am i doing wrong?
encoding='mbcs'
instead to get the default Windows locale encoding.chcp
at a command prompt.