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

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

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

問題描述

您可以使用 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

產量:

$ 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
...

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

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.

有沒有一種可移植的方式來獲取一個包含目錄列表的數組?

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

(數組應該只有文件夾名稱.)

(The array should only have the folder names.)

推薦答案

嘗試使用 ftp.nlst(dir).

但請注意,如果文件夾為空,則可能會引發錯誤:

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

這篇關于使用 Python 的 ftplib 獲取目錄列表,可移植的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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:“浮動對象不可下標)
主站蜘蛛池模板: 五月婷婷 | 欧美性生交xxxxx | 日本成人中文字幕 | 亚洲成人免费在线观看 | 超碰91在线 | 国产成人精品亚洲男人的天堂 | 一区二区三区在线观看免费 | 精品国产一区二区在线观看 | 黄色片免费网站 | 欧美一区二区三区在线视频 | 羞羞的网站 | 国产小视频在线播放 | 国产三级在线播放 | 99视频网站 | 天天天天躁天天爱天天碰2018 | 亚洲女优在线 | 国语对白做受欧美 | 成年网站在线观看 | 欧美精品一二区 | 日韩欧美视频 | 色77777| 亚洲一区久久 | 青青伊人网| 亚洲黄色小视频 | a级片网站 | 色片在线观看 | 午夜av在线播放 | 欧美视频一区二区三区 | 一区二区三区在线看 | 欧美日韩在线免费观看 | 九九精品在线观看 | 国产美女一区 | 九九视频在线观看 | 欧美日韩一区二区在线 | 国产视频a | 国产一区二区中文字幕 | 中文字幕无人区二 | 91视频观看 | av一区二区三区四区 | 成人小网站| 亚洲第一在线 |