【问题标题】:Going through ftp directories in python在 python 中浏览 ftp 目录
【发布时间】:2014-03-22 18:33:29
【问题描述】:

我正在尝试使用 ftplib 从具有 Python 3 的 ftp 服务器下载几个文件夹。

我有一个文件夹名称列表。它们都位于文件夹“根”中。问题是我不知道如何浏览它们。当我使用cwd时,我可以进入更深的目录,但是我怎么又起来了呢?

我正在尝试获得类似的东西

list = ["folder1", "folder2", "folder3"]
for folder in list:
   ##navigate to folder
   ##do something

【问题讨论】:

  • ftp_object.cwd('..') 带你上一个目录。

标签: python ftp ftplib


【解决方案1】:

您可以使用FTP.pwd 方法检索当前目录。在更改目录之前记住该目录。

parent_dir = ftp_object.pwd()
list = ["folder1", "folder2", "folder3"]
for folder in list:
    ftp_object.cwd('{}/{}'.format(parent_dir, folder))
ftp_object.cwd(parent_dir) # go to parent directory

【讨论】:

    【解决方案2】:

    我对找到的代码进行了一些更改here

    您必须在运行代码之前创建目标文件夹。 另外,我使用的网站不需要用户名或密码。

    请让我知道这是否有效。我想知道我是否应该“把它放在我的后兜里”并将它保存到我的外部硬盘驱动器中。

        #!/usr/bin/python
    
    import sys
    import ftplib
    import urllib.request
    import os
    import time
    import errno
    
    server = "ftp.analog.com"
    #user = ""
    #password = ""
    source = "/pub/MicroConverter/ADuCM36x/"
    destination0 = "C:/NewFolder/" # YOU HAVE TO UT THIS NEW FOLDER IN C: BEFORE RUNNING
    interval = 0.05
    
    ftp = ftplib.FTP(server)
    ftp.login()#(user, password)
    
    count = 0 #We need this variable to make the first folder correctly
    
    def downloadFiles(path, destination):
        try:
            ftp.cwd(path)       
            os.chdir(destination)
            mkdir_p(destination[0:len(destination)-1] + path)
            print ("Created: " + destination[0:len(destination)-1] + path )
        except OSError:     
            pass
        except ftplib.error_perm:       
            print ( "Error: could not change to " + path )
            sys.exit("Ending Application")
    
        filelist=ftp.nlst()
    
        print(filelist)
    
    
        for file in filelist:
            time.sleep(interval)
    
            if "." in file :
                url = ("ftp://" + server + path + file)
                urllib.request.urlretrieve(url, destination + path + file)
    
            else:
                try:            
                    ftp.cwd(path + file + "/")
    
    
                    downloadFiles(path + file + "/", destination)
                except ftplib.error_perm:
                    os.chdir(destination[0:len(destination)-1] + path)
    
                    try:
                        ftp.retrbinary("RETR " + file, open(os.path.join(destination + path, file),"wb").write)
                        print ("Downloaded: " + file)
                    except:
                        print ("Error: File could not be downloaded " + file)
    
        return
    
    
    def mkdir_p(path):
        try:
            os.makedirs(path)
        except OSError as exc:
            if exc.errno == errno.EEXIST and os.path.isdir(path):
                pass
            else:
                raise
    
    
    downloadFiles(source, destination0)
    
    #ftp.quit()
    

    【讨论】:

    • 向提问者提供完整代码通常是不受欢迎的。尽管如此,添加一些细节来说明为什么您的代码可能是更好的选择会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2015-09-14
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    相关资源
    最近更新 更多