FTP 使用 python 上传,rb 模式有问题

FTP upload using python, having trouble with rb mode

我有一个 zip 文件要上传。我知道如何上传 it.I 用 "Rb" 模式打开文件。当我想提取我上传的 zip 文件时,出现错误并且 ZIP 存档中的文件消失了,我认为这是因为 "Rb" 模式。我不知道如何提取我上传的文件。

代码如下:

filename="test.zip"
ftp=ftplib.FTP("ftp.test.com")
ftp.login('xxxx','xxxxx')
ftp.cwd("public_html/xxx")
myfile=open("filepath","rb")
ftp.storlines('STOR ' + filename,myfile)
ftp.quit()
ftp.close()

您的代码目前正在使用 ftp.storlines(),它旨在用于 ASCII 文件。

对于ZIP文件等二进制文件,您需要使用ftp.storbinary()代替:

import ftplib

filename = "test.zip"    

with open(filename, 'rb') as f_upload:
    ftp = ftplib.FTP("ftp.test.com")
    ftp.login('xxxx', 'xxxxx')
    ftp.cwd("public_html/xxx")
    ftp.storbinary('STOR ' + filename, f_upload)
    ftp.quit()
    ftp.close()    

当在 ZIP 文件上使用 ASCII 模式时,它会导致文件无法使用,这就是您得到的文件。