使用python进行FTP上传,遇到rb模式问题

问题描述 投票:-2回答:1

我有一个要上传的zip文件。我知道如何上传它。我用“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()
python zipfile ftplib
1个回答
0
投票

您的代码目前正在使用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模式时,将导致文件无法访问。

© www.soinside.com 2019 - 2024. All rights reserved.