简体   繁体   中英

Python: Reading Ftp file list with UTF-8?

Hi I am using module ftplib. And list my files with this code:

files=[]
files = ftp.nlst()

And write them to text file with this code:

for item in files:
    filenames.write(item +'\n')

But there is an encoding problem that if my file name has 'ı,ğ,ş' characters, It cant read this and writes to file with '?' instead.

How can read them properly?

Python 3.x is using default encoding ISO-8859-1 for file name.

To use UTF-8 encoding for file name with the server, you need to add the following line:

ftpConnector = ftplib.FTP(host,user,password) # connection

ftpConnector.encoding='utf-8' #force encoding for file name in utf-8 rather than default that is iso-8889-1

then you can use:

ftpConnector.storbinary( 'STOR '+fileName, file) # filename will be utf-8 encoded

You need to convert the resulting items back to unicode before writing the items to file. The ftp module does not support unicode strings. To do this try:

import encodings.idna

for item in files:
    filenames.write(encodings.idna.ToUnicode(item) + '\n')
ftp.encoding='utf-8'
ftp.sendcmd('OPTS UTF8 ON')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM