5

There are many posts about 'latin-1' codec , however those answers can't solve my problem, maybe it's my question, I am just a rookie to learn Python, somewhat. When I used cwd(dirname) to change directory of FTP site, the unicodeerror occurred. Note that dirname included Chinese characters, obviously, those characters caused this error. I made some encoding and decoding according to the suggestions in the past posts, but it didn't work. Could someone give me some advice how to repair this error and make cwd work?

Some codes:

file = 'myhongze.jpg'
dirname = './项目成员资料/zgcao/test-python/'
site = '***.***.***.***'
user = ('zhigang',getpass('Input Pwd:'))    
ftp = FTP(site)
ftp.login(*user)            
ftp.cwd(dirname)# throw exception

Some tests:

u'./项目成员资料/zgcao/test-python/'.encode('utf-8')

Output:

b'./\xe9\xa1\xb9\xe7\x9b\xae\xe6\x88\x90\xe5\x91\x98\xe8\xb5\x84\xe6\x96\x99/zgcao/test-python/'

u'./项目成员资料/zgcao/test-python/'.encode('utf-8').decode('cp1252')

Output:

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

u'./项目成员资料/zgcao/test-python/'.encode('utf-8').decode('latin-1')

Output:

'./项ç\x9b®æ\x88\x90å\x91\x98èµ\x84æ\x96\x99/zgcao/test-python/'
Using the result of decode('latin-1'), the cwd can't still work.

It is noted that 项目成员资料 is showed as ÏîÄ¿×é³ÉԱ˽È˿ռä when I used retrlines('LIST').

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
zhigang
  • 66
  • 1
  • 3

3 Answers3

2

No need to edit ftplib source code. Just set ftp.encoding property in your code:

ftp.encoding = "UTF-8"
ftp.cwd(dirname)

A similar question, about FTP output, rather then input:
List files with UTF-8 characters in the name in Python ftplib

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

I solved this problem by editing ftplib.py. On my machine, it is under C:\Users\<user>\AppData\Local\Programs\Python\Python36\Lib.

You just need to replace encoding = "latin-1" with encoding = "utf-8"

  • Reposted from a comment by Andrew zhong: *After changing the ftplib.py encoding type, I downloaded the file successfully, but the encoding type should be what fits your ftp server, not just utf-8, in my case which is Windows NT with encoding type GBK, so the encoding = "gbk".* – karel Jul 12 '19 at 01:04
  • Thanks for Stephen Hoo's answer,After changed the ftplib.py encoding type, I've downloaded the file successfull. But the encoding type should be fits your ftp server, not just `utf-8`, in my case which is Windows NT with encoding type GBK, so the `encoding = "gbk" ` – Andrew zhong Jul 12 '19 at 00:01
0

I suggest that you do not change the source code of the package, just reload the ftp object and modify the encoding type, for example:

from ftplib import FTP
ftp = FTP()
ftp.encoding = 'utf-8' # Here you can modify the encoding property of the object
JustDoIt
  • 141
  • 7