久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

遍歷 FTP 列表

Traversing FTP listing(遍歷 FTP 列表)
本文介紹了遍歷 FTP 列表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試從 FTP 服務器獲取所有目錄的名稱,并將它們以分層順序存儲在多維列表或字典中

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']
    ]
  ]
]

我嘗試過使用這樣的遞歸函數:def 遍歷(目錄):FTP.dir(目錄,遍歷)

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

FTP.dir 以這種格式返回行:

FTP.dir returns lines in this format:

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

這樣做 line[56:] 只會給我目錄名稱(mysite.com).我在遞歸函數中使用它.

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

但我無法讓它工作.我嘗試了許多不同的方法,但無法讓它發揮作用.還有很多 FTP 錯誤(找不到目錄 - 這是一個邏輯問題,有時服務器返回意外錯誤,沒有留下日志,我無法調試)

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)

底線問題:如何從 FTP 服務器獲取分層目錄列表?

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

推薦答案

這是一個幼稚而緩慢的實現.它很慢,因為它嘗試對每個目錄條目進行 CWD 以確定它是目錄還是文件,但這有效.可以通過解析 LIST 命令輸出來優化它,但這很大程度上依賴于服務器實現.

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 列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 一区二区三区福利视频 | 一区二区三区视频免费看 | 国产一区二区三区四区在线观看 | 亚洲电影专区 | 久久久久久美女 | 亚洲自拍偷拍欧美 | 日韩中文在线视频 | 伊人久久精品一区二区三区 | 亚洲欧美精品久久 | 丁香五月网久久综合 | 欧美亚洲国产一区二区三区 | 久久久久久久久久久丰满 | 日本一区二区视频 | 国产精品7777777 | 亚洲 精品 综合 精品 自拍 | 国产在线观看免费 | 久久国产成人午夜av影院武则天 | 91网站在线看 | 亚洲精品在线视频 | 久久天堂网| 一区二区三区国产好 | 日韩 国产 在线 | 久草www| 中文字幕日韩在线 | 精品国产欧美在线 | 在线成人av | 国产精品99久久久久久久久久久久 | 欧美在线免费 | 在线观看亚洲专区 | 久久久久国产视频 | ririsao久久精品一区 | 日韩在线不卡视频 | 亚洲精品字幕 | 国产在线观看一区 | 欧美日韩福利视频 | 日韩一区二区在线免费观看 | 日韩欧美中文字幕在线观看 | 欧美一区二区三区在线视频 | 久久久久久国产精品免费免费狐狸 | 成人免费观看男女羞羞视频 | 国产精品久久久久久久岛一牛影视 |