Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 668
Busra Koken Avatar asked Apr 08 '15 18:04

Busra Koken


1 Answers

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
like image 100
user5445419 Avatar answered Sep 25 '22 23:09

user5445419