简体   繁体   English

文件名中的一些字母(ü,ä)在上传到 Python 中的 FTP 后变成乱码

[英]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.我正在尝试将图像文件上传到 FTP 服务器,并且文件名具有西欧字符。

file_name = 'müde_Mäuschen'

If I upload this file to FTP it changes letters.如果我将此文件上传到 FTP 它会更改字母。 File name in FTP becomes müde_Mäuschen . FTP 中的文件名变为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.如果我使用 FileZilla 将文件上传到 FTP,那么名称将完美运行,因此 FTP 服务器确实支持此欧洲字母。
  2. I'm on Windows 10, Python 3.9.0我在 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"))

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. 我不知道目标系统使用的是什么编码,但它看起来像是 Windows 机器,而 cp1252 是西欧 Windows 机器上的常见编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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