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

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

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

問題描述

您可以使用 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ù)測 FTP 服務(wù)器可能會格式化此列表的所有不同方式,為此編寫代碼會非常麻煩.

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.

有沒有一種可移植的方式來獲取一個(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).

但請注意,如果文件夾為空,則可能會引發(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 獲取目錄列表,可移植的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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 的開頭(最左端)插入一列)
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:“浮動對象不可下標(biāo))
主站蜘蛛池模板: 97人人干 | 亚洲97| 四虎成人免费视频 | 国产激情一区二区三区 | 久久久国产一区二区三区 | 亚洲精品av在线 | 亚洲国产一 | 免费人成激情视频在线观看冫 | 羞羞色在线观看 | 国产一区影院 | 亚洲国产成人精品女人久久久野战 | 日韩色图视频 | 日韩视频专区 | 国产精品久久久久久久久久久久 | 婷婷中文在线 | a级黄色片在线观看 | 亚洲精品免费在线 | 羞羞网站免费 | 日韩中文一区 | 国产一区二区三区久久久久久久久 | www.9191| 国产精品一区二区无线 | 成人天堂| 国产精品美女久久久久久免费 | 亚洲高清视频一区二区 | 国产一区二区三区久久久久久久久 | xnxx 日本免费 | 激情国产在线 | 成人a免费| 久久久精品黄色 | 精品一区二区三 | 欧美精品一区二区三区在线播放 | 欧美日韩久| av电影手机在线看 | 成人在线中文字幕 | 国产在线精品一区二区三区 | 欧美日韩网站 | 成年人黄色一级毛片 | 懂色一区二区三区免费观看 | 日韩精品久久一区二区三区 | 五月激情综合 |