问题描述
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设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)