2

I wrote a very simple program in Python that shows names of the folders in a specific direction on my computer. The program performs well, but the problem is that when I have a file or folder named with Persian characters and running the program in Windows cmd or PowerShell, they show the characters in this form: ?

I investigated this problem and there were suggestions like typing chcp 65001 command, but when I do this and retry the program, I get this error:

Traceback (most recent call last):
  File "p.py", line 39, in <module>
    g=(path_dic(a))
  File "p.py", line 23, in path_dic
    print('%d : %s'%(i,dic[i]))
PermissionError: [WinError 31] A device attached to the system is not functioning
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
PermissionError: [WinError 31] A device attached to the system is not functioning

By the way: When I run the program in my IDE (pycharm), it displays the characters without problem, but I'm curious to see if there is a way to display Persian characters in Windows cmd or PowerShell?

I have also done changing the font to consolas or lucida and still not working

M.H Mighani
  • 196
  • 5
  • 19
  • 2
    See my answer on [Using another language (code page) in a batch file made for others](https://stackoverflow.com/a/48982681/3074564) and the comments below the answer. For usage of UTF-8 in a command prompt window or in PowerShell it is additionally necessary to use a font supporting Unicode like Consolas being default since Windows 8. There is unfortunately no command to change from within a batch file the font used for already opened console window. See also search results for: [\[python\] Windows encoding console](https://stackoverflow.com/search?q=%5Bpython%5D+Windows+encoding+console). – Mofi Apr 28 '18 at 09:26
  • thanks mofi, but i have also done changing font from rasters to consolas or lucida, but it still not working,maybe it's because i'm using widows 7. – M.H Mighani Apr 28 '18 at 09:51
  • So you have opened a command prompt window, clicked on icon on top left corner of the window and clicked on __Properties__, selected as font *Lucida Console* or *Consolas* and clicked __OK__. Next you have typed `chcp 65001` in same command prompt window. And last you have executed the Python script written by you. Is that right? I am not a Python script programmer. So I can't help you on coding your Python script to detect encoding of handle `stdout` and prepare strings to output accordingly before printing them to `stdout`. I know how to do this in C/C++, but not in Python. – Mofi Apr 28 '18 at 10:08
  • i exactly did what you said step by step and it did not work, i should try this in a upper version windows , maybe it will work, thanks mofi – M.H Mighani Apr 28 '18 at 10:15

0 Answers0