Raspberry Pi using Python for mkdir

I want to download all images from a website using Python. I already have the header of the HTML file, so I use os.mkdir()to create the folder, but the header contains Chinese. When the following code is executed on the Raspberry Pi (OS: Debian), an exception is thrown:

UnicodeEncodeError: ascii codec cannot encode characters at positions 20-38: serial number not in range (128)

try:
    os.mkdir(path)
except:
    print "create folder failed"
    continue

Everything is fine if I run the code on my Mac.

I searched for the problem and tried the code below, but it does not work.

reload(sys)
sys.setdefaultencoding('utf-8')

Update:

I changed my code a lot and read some articles about unicode, utf-8.etc. But every time I run the code on Raspberry Pi, it is crushed. Then I installed the Debian OS (which I installed on my Raspberry Pi) on the PC, ran the code on it, everything is fine.

, , SSH.

Debian , . Mac SSH , , "Unicode..." - .

, , : -)

+4
4

r u ,

>>> import os
>>> os.mkdir(r'文件夹')
>>> os.mkdir(u'文件夹')

xxx/文件 夹/.

, .

0

setdefaultencoding print write - os.mkdir( path.encode('utf-8') )

-

BTW: print sys.getfilesystemencoding() - - utf8, , , LANG Bash. Bash echo $LANG. - utf8, export LANG=en_GB.utf8 Bash python script encode(). , export LANG=en_GB.utf8 ~/.bashrc ~/.profile , , /etc/profile

locale locales raspberry

0

, , : -)

, , LC_CTYPE "***.UTF-8"

Debian , Debian . vps (Ubuntu ), Mac (Mac OS X ). , enter image description here

, LC_CTPPE "***.UTF-8", , . - , .

:

  • , , .

  • LC_CTYPE "*.UTF-8", . , *.UTF-8, it "*.UTF-8", . , , UTF-8 .

  • , iTerm, , .

, .

: , , .

LC_CTYPE UTF-8 , , ASCII. , python unicode, . print, python LC_CTYPE, . unicode 127, DUANG! DUANG! DUANG, .

0

, Python 2, script , :

from __future__ import unicode_literals

2 sys.

This will allow you to use unicode strings in your script by default, so any line that is received as a result or defined explicitly (even without u), for example s="string", will be a unicode string and therefore will support Chinese characters.

-1
source

Source: https://habr.com/ru/post/1626715/


All Articles