問題描述
Python 有一個標準庫模塊 ftplib
來運行 FTP 通信.它有兩種獲取目錄內容列表的方法.一,FTP.nlst()
,將返回一個目錄的內容列表給定一個目錄名稱作為參數.(如果給定文件名,它將返回文件名.)這是一種列出目錄內容的可靠方法,但不提供任何指示列表中的每個項目是文件還是目錄.另一種方法是 FTP.dir()
,它以字符串格式列出作為參數給出的目錄的目錄內容(或文件屬性,給定文件名).
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).
根據 上一個關于 Stack Overflow 的問題,解析dir()
的結果可能很脆弱(不同的服務器可能返回不同的字符串).不過,我正在尋找某種方法來僅列出通過 FTP 包含在另一個目錄中的目錄.據我所知,在字符串的權限部分中抓取 d
是我想出的唯一解決方案,但我想我不能保證權限會出現在不同服務器之間的同一個地方.是否有更強大的解決方案來通過 FTP 識別目錄?
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() 獲得的結果將是最好的".
Unfortunately FTP doesn't have a command to list just folders so parsing the results you get from ftp.dir() would be 'best'.
一個簡單的應用程序,假設來自 ls(不是 windows ftp)的標準結果
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
標準 FTP 命令
自定義 FTP 命令
這篇關于通過 FTP 確定列表是否是 Python 中的目錄或文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!