【问题标题】:How to Retrieve a Zip Folder from FTP in Python如何在 Python 中从 FTP 检索 Zip 文件夹
【发布时间】:2014-09-09 12:11:15
【问题描述】:

我正在尝试使用 python 从 ftp 站点检索一个 zip 文件夹并将它们保存到我的本地计算机(理想情况下,我想指定它们在我的 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")

【问题讨论】:

    标签: python ftp zip


    【解决方案1】:

    好吧,看来您只是忘记打开要写入的文件了。

    类似:

    getbinary(ftp, "subbdy.zip", open(r'C:\Path\to\subbdy.zip', 'wb'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多