FTP 损坏的 ZIP
当我使用此代码从 FTP 服务器下载 zip 文件时,它返回时已损坏。有人知道为什么吗?
from ftplib import FTP
import getpass
user = raw_input('Username: ')
password = getpass.getpass()
host = raw_input('Host:')
ftp = FTP(host,user,password)
ftp.retrlines('LIST')
f_file = raw_input('What is the name of the file you would like to download? ')
print 'Opening local file...'
l_file = open(f_file, 'w')
print "Getting", f_file
ftp.retrbinary('RETR ' + f_file, l_file.write)
print "Closing", f_file
l_file.close()
print 'Closing FTP connection'
ftp.close()
When I use this code to download a zip file from and FTP server it comes back corrupted. Anyone know why?
from ftplib import FTP
import getpass
user = raw_input('Username: ')
password = getpass.getpass()
host = raw_input('Host:')
ftp = FTP(host,user,password)
ftp.retrlines('LIST')
f_file = raw_input('What is the name of the file you would like to download? ')
print 'Opening local file...'
l_file = open(f_file, 'w')
print "Getting", f_file
ftp.retrbinary('RETR ' + f_file, l_file.write)
print "Closing", f_file
l_file.close()
print 'Closing FTP connection'
ftp.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为您以 ASCII 模式而不是二进制模式写入本地副本,从而将所有
0A
字节更改为0D0A
(LF 到 CRLF),从而损坏了二进制文件。请使用
l_file = open(f_file, 'wb')
重试。This is probably because you're writing the local copy in ASCII mode, not binary mode, thereby changing all
0A
bytes into0D0A
(LF to CRLF), corrupting the binary file.Try again using
l_file = open(f_file, 'wb')
.