本文介绍了遍历FTP列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从FTP服务器获取所有目录的名称,并将它们按层级顺序存储在多维列表或字典中。例如,一台服务器包含以下结构:

  / www / 
mysite.com
图片
png
jpg

在脚本结尾处,会给我一个列表,例如 p>

  ['/ www /'
['mysite.com'
['images'
['png'],
['jpg']
]
]
]

我曾尝试使用递归函数,如下所示:
def traverse(dir):
FTP.dir(dir,遍历)

FTP.dir以这种格式返回行:

  drwxr-xr-x 5 leavesc1 leavesc1 4096 Nov 29 20:52 mysite.com 

这样做行[56:]会给我只是目录名mysite.com)。我在递归函数中使用它。



但是我无法使它工作。我尝试了许多不同的方法,但无法实现。很多的FTP错误(无论是找不到目录 - 这是一个合乎逻辑的问题,有时服务器返回的意外错误,没有记录,我无法调试)

底线问题:
如何从FTP服务器获取分层目录列表?

解决方案

这是一个天真而缓慢的实施。它很慢,因为它试图对每个目录条目进行CWD以确定它是目录还是文件,但是这是有效的。可以通过解析LIST命令输出来优化它,但这是强烈依赖于服务器实现的。

  import ftplib 

def traverse(ftp,depth = 0):

返回一个ftp服务器内容的递归列表(从当前目录开始


列表作为递归字典返回,其中每个键
包含子目录的内容,如果
与文件相对应,则包含无。

@param ftp:ftplib。 FTP对象

if depth> 10:
return ['depth> ('。','..')):
尝试:如果路径不在(ftp.nlst()路径中,
ftp.cwd(entry)
level [entry] =遍历(ftp,depth + 1)
ftp.cwd('..')
除了ftplib.error_perm:
level [entry] = None
返回级别

def main():
ftp = ftplib.FTP(localhost)
ftp.connect )
ftp.login()
ftp.set_pasv(True)

print traverse(ftp)

if __name__ =='__main__':
main()


I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict

So for example, a server that contains the following structure:

/www/
    mysite.com
        images
            png
            jpg

at the end of the script, would give me a list such as

['/www/'
  ['mysite.com'
    ['images'
      ['png'],
      ['jpg']
    ]
  ]
]

I have tried using a recursive function like so:def traverse(dir): FTP.dir(dir, traverse)

FTP.dir returns lines in this format:

drwxr-xr-x    5 leavesc1 leavesc1     4096 Nov 29 20:52 mysite.com

so doing line[56:] will give me just the directory name(mysite.com). I use this in the recursive function.

But i cannot get it to work. I've tried many different approaches and can't get it to work. Lots of FTP errors as well (either can't find the directory - which is a logical issue, and sometimes unexpected errors returned by the server, which leaves no log and i can't debug)

bottom line question:How to get a hierarchical directory listing from an FTP server?

解决方案

Here is a naive and slow implementation. It is slow because it tries to CWD to each directory entry to determine if it is a directory or a file, but this works. One could optimize it by parsing LIST command output, but this is strongly server-implementation dependent.

import ftplib

def traverse(ftp, depth=0):
    """
    return a recursive listing of an ftp server contents (starting
    from the current directory)

    listing is returned as a recursive dictionary, where each key
    contains a contents of the subdirectory or None if it corresponds
    to a file.

    @param ftp: ftplib.FTP object
    """
    if depth > 10:
        return ['depth > 10']
    level = {}
    for entry in (path for path in ftp.nlst() if path not in ('.', '..')):
        try:
            ftp.cwd(entry)
            level[entry] = traverse(ftp, depth+1)
            ftp.cwd('..')
        except ftplib.error_perm:
            level[entry] = None
    return level

def main():
    ftp = ftplib.FTP("localhost")
    ftp.connect()
    ftp.login()
    ftp.set_pasv(True)

    print traverse(ftp)

if __name__ == '__main__':
    main()

这篇关于遍历FTP列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:43