如何用Python从FTP获取ZIP文件夹
我想从一个FTP网站上下载一个或多个压缩文件,并把它们保存到我的电脑上,最好是能指定保存到C盘的某个位置。
下面的代码连接到了FTP网站,然后在PyScripter窗口里出现了大约1000行看起来像随机字符的东西……但是实际上并没有什么文件下载到我的硬盘上。
有没有什么建议呢?
import ftplib
import sys
def gettext(ftp, filename, outfile=None):
# fetch a text file
if outfile is None:
outfile = sys.stdout
# use a lambda to add newlines to the lines read from the server
ftp.retrlines("RETR " + filename, lambda s, w=outfile.write: w(s+"\n"))
def getbinary(ftp, filename, outfile=None):
# fetch a binary file
if outfile is None:
outfile = sys.stdout
ftp.retrbinary("RETR " + filename, outfile.write)
ftp = ftplib.FTP("FTP IP Address")
ftp.login("username", "password")
ftp.cwd("/MCPA")
#gettext(ftp, "subbdy.zip")
getbinary(ftp, "subbdy.zip")
1 个回答
2
看起来你只是忘了打开你想要写入的文件。
像这样:
getbinary(ftp, "subbdy.zip", open(r'C:\Path\to\subbdy.zip', 'wb'))