久久久久久久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))
主站蜘蛛池模板: 犬夜叉在线观看 | 免费高清成人 | 国产精品电影网 | 午夜视频免费在线观看 | 中文字幕在线免费观看 | 丁香久久 | 久久久久久久久精 | 日韩在线看片 | 亚洲一区电影 | 亚洲精品中文字幕在线观看 | 久久91av | 欧美激情五月 | 久久久激情 | 国产美女久久久 | 天天艹逼网 | 日韩精品视频在线 | 成人国产精品久久 | 亚洲视频中文字幕 | 在线观看视频91 | 精品视频网 | 色悠悠久| 性一交一乱一伦视频免费观看 | 成人在线免费观看视频 | 亚洲国产精品一区二区三区 | 国产精品日韩一区二区 | 国产精品久久久久久久久久三级 | 日韩欧美国产不卡 | 亚洲成人免费观看 | 日韩精品一区二区三区在线 | 欧美成人精品一区 | 国产精品久久久久久久久久久久午夜片 | 欧美久久久久久久久 | 亚洲精品高清视频 | 亚洲97 | 欧美a∨ | 99久久精品免费看国产四区 | 亚洲视频在线观看免费 | 国产精品一区二区欧美黑人喷潮水 | 久久久精品网站 | 欧美在线 | 亚洲网站观看 |