問(wèn)題描述
Python 有一個(gè)標(biāo)準(zhǔn)庫(kù)模塊 ftplib
來(lái)運(yùn)行 FTP 通信.它有兩種獲取目錄內(nèi)容列表的方法.一,FTP.nlst()
,將返回一個(gè)目錄的內(nèi)容列表給定一個(gè)目錄名稱作為參數(shù).(如果給定文件名,它將返回文件名.)這是一種列出目錄內(nèi)容的可靠方法,但不提供任何指示列表中的每個(gè)項(xiàng)目是文件還是目錄.另一種方法是 FTP.dir()
,它以字符串格式列出作為參數(shù)給出的目錄的目錄內(nèi)容(或文件屬性,給定文件名).
Python has a standard library module ftplib
to run FTP communications. It has two means of getting a listing of directory contents. One, FTP.nlst()
, will return a list of the contents of a directory given a directory name as an argument. (It will return the name of a file if given a file name instead.) This is a robust way to list the contents of a directory but does not give any indication whether each item in the list is a file or directory. The other method is FTP.dir()
, which gives a string formatted listing of the directory contents of the directory given as an argument (or of the file attributes, given a file name).
根據(jù) 上一個(gè)關(guān)于 Stack Overflow 的問(wèn)題,解析dir()
的結(jié)果可能很脆弱(不同的服務(wù)器可能返回不同的字符串).不過(guò),我正在尋找某種方法來(lái)僅列出通過(guò) FTP 包含在另一個(gè)目錄中的目錄.據(jù)我所知,在字符串的權(quán)限部分中抓取 d
是我想出的唯一解決方案,但我想我不能保證權(quán)限會(huì)出現(xiàn)在不同服務(wù)器之間的同一個(gè)地方.是否有更強(qiáng)大的解決方案來(lái)通過(guò) FTP 識(shí)別目錄?
According to a previous question on Stack Overflow, parsing the results of dir()
can be fragile (different servers may return different strings). I'm looking for some way to list just the directories contained within another directory over FTP, though. To the best of my knowledge, scraping for a d
in the permissions part of the string is the only solution I've come up with, but I guess I can't guarantee that the permissions will appear in the same place between different servers. Is there a more robust solution to identifying directories over FTP?
推薦答案
不幸的是 FTP 沒有列出文件夾的命令,因此解析從 ftp.dir() 獲得的結(jié)果將是最好的".
Unfortunately FTP doesn't have a command to list just folders so parsing the results you get from ftp.dir() would be 'best'.
一個(gè)簡(jiǎn)單的應(yīng)用程序,假設(shè)來(lái)自 ls(不是 windows ftp)的標(biāo)準(zhǔn)結(jié)果
A simple app assuming a standard result from ls (not a windows ftp)
from ftplib import FTP
ftp = FTP(host, user, passwd)
for r in ftp.dir():
if r.upper().startswith('D'):
print r[58:] # Starting point
標(biāo)準(zhǔn) FTP 命令
自定義 FTP 命令
這篇關(guān)于通過(guò) FTP 確定列表是否是 Python 中的目錄或文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!