【问题标题】:When uploading recursively all files to FTP using ftplib, only the first directory is successfully transferred使用 ftplib 递归上传所有文件到 FTP 时,只有第一个目录传输成功
【发布时间】:2021-06-09 10:39:21
【问题描述】:

我正在尝试将包含文件和子目录的所有目录从 FTP 服务器复制到本地目录。目标是在程序第一次执行时创建所有这些副本,并在其他执行时更新更改的副本或添加新添加的副本。

我的FTP目录结构是:

├── _directory1
│   ├──_subdirectory1
│       ├── file1.py
│       ├── file2.py
│   ├──_subdirectory2
│       ├── file3.py
│       ├── file4.py│   
|   ├──_subdirectory3
│       ├── file3.py
│       ├── file4.py
│   └── packages.py
│   └── somefile.py
│   └── somepdf.pdf
├── _directory2
│   ├──_subdirectory2.1
│       ├── file2.1.py

这是我的代码:

import os
import os.path
from ftplib import FTP, error_perm
from io import BytesIO

host = 'localhost'
username = 'user'
password = 'password'
port = 21
ftp = FTP()
ftp.connect(host, port)
ftp.login(username, password)
filenameCV = "C:/Users/User/Desktop/test"
# ftp.set_debuglevel(2)


def copy_all_files(ftp_server, path):
    for filename in ftp_server.nlst():
        print(filename)
        try:
            ftp_server.size(filename)
            if os.path.exists(os.path.join(path, filename)):
                with open(os.path.join(path, filename), "rb") as file:
                    r = BytesIO()
                    try:
                        ftp_server.retrbinary('RETR ' + filename, r.write)
                        if BytesIO(file.read()).getvalue() != r.getvalue():
                            print(filename+"has changed")
                            ftp_server.retrbinary('RETR ' + filename,
                                                  open(os.path.join(path, filename),
                                                       'wb').write)
                    except Exception as ee:
                        print(ee)
            else:
                ftp_server.retrbinary('RETR ' + filename, open(os.path.join(path, filename), 'wb').write)
        except:
            try:
                if not os.path.exists(os.path.join(path, filename)):
                    os.mkdir(os.path.join(path, filename))
                ftp_server.cwd(filename)
                copy_all_files(ftp_server, os.path.join(path, filename))
            except:
                pass


copy_all_files(ftp, filenameCV)
ftp.quit()

问题是我的代码从 FTP 创建目录 1 和目录 2,但只复制和更改子目录 1 和其中的文件,其余目录为空,目录 1 级别的文件丢失,子目录 2,3并且目录 2 是空的,并且目录级别的 pdf 也被复制为目录。我是否需要更改 ftp.cwd 或者没有被复制其他目录的原因是什么?

【问题讨论】:

    标签: python ftp ftplib


    【解决方案1】:

    您在此处输入远程文件夹:

    ftp_server.cwd(filename)
    

    但你永远不会离开他们。


    要么:

    • 下载内容后离开该文件夹:

      ftp_server.cwd(filename)
      copy_all_files(ftp_server, os.path.join(path, filename))
      ftp_server.cwd("..")
      
    • 或者在进入文件夹时使用绝对路径。这样cwd 就可以工作,无论你在那一刻。喜欢这里:
      Downloading a directory tree with ftplib

      从广义上讲,您的问题实际上是上述问题的重复——您为什么要尝试实施一些已经有可行解决方案的东西?

    【讨论】:

    • 非常感谢!真的很有帮助。我添加了 ftp_server.cwd("..") 并且它现在可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2021-04-09
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多