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

使用 Python 的 ftplib 獲取目錄列表,可移植

Using Python#39;s ftplib to get a directory listing, portably(使用 Python 的 ftplib 獲取目錄列表,可移植)
本文介紹了使用 Python 的 ftplib 獲取目錄列表,可移植的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

您可以使用 ftplib 在 Python 中獲得完整的 FTP 支持.然而,獲取目錄列表的首選方式是:

You can use ftplib for full FTP support in Python. However the preferred way of getting a directory listing is:

# File: ftplib-example-1.py

import ftplib

ftp = ftplib.FTP("www.python.org")
ftp.login("anonymous", "ftplib-example-1")

data = []

ftp.dir(data.append)

ftp.quit()

for line in data:
    print "-", line

產(chǎn)量:

$ python ftplib-example-1.py
- total 34
- drwxrwxr-x  11 root     4127         512 Sep 14 14:18 .
- drwxrwxr-x  11 root     4127         512 Sep 14 14:18 ..
- drwxrwxr-x   2 root     4127         512 Sep 13 15:18 RCS
- lrwxrwxrwx   1 root     bin           11 Jun 29 14:34 README -> welcome.msg
- drwxr-xr-x   3 root     wheel        512 May 19  1998 bin
- drwxr-sr-x   3 root     1400         512 Jun  9  1997 dev
- drwxrwxr--   2 root     4127         512 Feb  8  1998 dup
- drwxr-xr-x   3 root     wheel        512 May 19  1998 etc
...

我想這個(gè)想法是解析結(jié)果以獲取目錄列表.但是,此列表直接取決于 FTP 服務(wù)器格式化列表的方式.必須預(yù)測(cè) FTP 服務(wù)器可能會(huì)格式化此列表的所有不同方式,為此編寫代碼會(huì)非常麻煩.

I guess the idea is to parse the results to get the directory listing. However this listing is directly dependent on the FTP server's way of formatting the list. It would be very messy to write code for this having to anticipate all the different ways FTP servers might format this list.

有沒(méi)有一種可移植的方式來(lái)獲取一個(gè)包含目錄列表的數(shù)組?

Is there a portable way to get an array filled with the directory listing?

(數(shù)組應(yīng)該只有文件夾名稱.)

(The array should only have the folder names.)

推薦答案

嘗試使用 ftp.nlst(dir).

但請(qǐng)注意,如果文件夾為空,則可能會(huì)引發(fā)錯(cuò)誤:

However, note that if the folder is empty, it might throw an error:

files = []

try:
    files = ftp.nlst()
except ftplib.error_perm as resp:
    if str(resp) == "550 No files found":
        print "No files in this directory"
    else:
        raise

for f in files:
    print f

這篇關(guān)于使用 Python 的 ftplib 獲取目錄列表,可移植的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開(kāi)頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒(méi)有插入到 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:“浮動(dòng)對(duì)象不可下標(biāo))
主站蜘蛛池模板: 久久久久国产一区二区三区 | 国产成人叼嘿视频在线观看 | 亚洲天堂男人的天堂 | 操网站 | 麻豆一区二区三区 | 99视频在线 | 亚洲一区视频在线 | 毛片一区二区三区 | 免费在线看黄 | 国产丝袜一区二区三区免费视频 | 国产精品欧美精品 | 国产高清精品一区 | 91视频进入 | 日本五月婷婷 | 成人av片在线观看 | 亚洲精品二区 | 国产成人在线一区二区 | 亚洲国产成人av好男人在线观看 | 成人黄色电影免费 | 爱操av| 色资源在线 | 久久精品日产第一区二区三区 | 中文字幕不卡一区 | 91 久久| 免费a网 | 日韩欧美视频在线 | 国产三区在线观看视频 | 欧美精品第一页 | 日韩成人影院 | 精品日韩一区 | 久久一区精品 | 日日碰狠狠躁久久躁婷婷 | 欧美日韩看片 | 亚洲国产精品一区 | 狠狠干美女 | 91国内外精品自在线播放 | 午夜欧美一区二区三区在线播放 | 国偷自产av一区二区三区 | 中文字幕免费在线 | 成人免费看片又大又黄 | 日韩欧美在线不卡 |