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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With