使用python上传FTP,rb mod有问题

2024-04-20 12:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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()

Tags: 文件代码testcom错误模式ftplogin
1条回答
网友
1楼 · 发布于 2024-04-20 12:44:19

您的代码当前正在使用^{},这是用于ASCII文件的。你知道吗

对于二进制文件(如ZIP文件),需要改用^{}

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模式时,它将导致一个不可用的文件,这就是您得到的文件。你知道吗

相关问题 更多 >