Search code examples
pythoncharacter-encodingftpftplib

Some letters (ü, ä) in file name turning to gibberish after uploading to FTP in Python


I'm trying to upload image file to FTP server and filename has western European character.

file_name = 'müde_Mäuschen'

If I upload this file to FTP it changes letters. File name in FTP becomes müde_Mäuschen.

Notes:

  1. If I upload file to FTP using FileZilla then name works perfectly so FTP server do support this european letters.
  2. I'm on Windows 10, Python 3.9.0

Code:

from ftplib import FTP

ftp = FTP("Login Details")
path = r'D:\ftp_test'
file_name = 'müde_Mäuschen.jpg'

ftp.storbinary(f"STOR {file_name}", open(path+'\\'+file_name, "rb"))

Solution

  • You need to specify the filesystem encoding of the remote system:

    ftp = FTP('Login Details', encoding='cp1252')
    

    Otherwise filenames and directory names will be decoded with the default encoding of the local system.


    I don't know what encoding the destination system is using, but it looks like it's a Windows machine and cp1252 is a common encoding on Western European Windows machines.