【问题标题】:Download FTP Files and saveas using shutil, urllib, and contextlib in Python在 Python 中使用 shutil、urllib 和 contextlib 下载 FTP 文件和另存为
【发布时间】:2021-11-12 12:32:54
【问题描述】:

在问题here 中,我们学习了如何在python 中使用FTP 获取文件。答案是我的代码中的内容,也如下:

import shutil
import urllib.request as request
from contextlib import closing

with closing(request.urlopen('ftp://server/path/to/file')) as r:
    with open('file', 'wb') as f:
        shutil.copyfileobj(r, f)

我现在如何在本地保存这些文件?这个path/to/file 在嵌套目录中有一堆目录和文件。理想情况下,我想下载根目录并在其中包含所有文件和文件夹。

由于我是 Python 和 FTP 的新手,我不能 100% 确定我的问题是否尽可能清楚。请不要犹豫,在 cmets 中要求澄清,谢谢!

【问题讨论】:

    标签: python python-3.x ftp shutil


    【解决方案1】:

    你可以试试这个

    import ftplib
    def getFile(filename,folder,ipaddr,usr,pswd):
            ftp = ftplib.FTP(ipaddr)
            ftp.login(usr,pswd)
            ftp.cwd(folder)
            ftp.retrbinary("RETR " + filename, open(filename, 'wb').write)
            ftp.quit()
    

    【讨论】:

    • 会试试的!目前使用 filezilla 作为概念证明,但它根本不可扩展。我应该把文件路径放在哪里?
    • 'Folder' 将是您的 ftp 路径。文件将保存到您的本地文件夹,位置与您的相同。 py 文件。如果要保存到特定位置,只需修改“open(filename_with_location,'wb')。写
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多