Determine if a listing is a directory or file in Python over FTP(通过 FTP 确定列表是否是 Python 中的目录或文件)
问题描述
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 中的目录或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过 FTP 确定列表是否是 Python 中的目录或文件
基础教程推荐
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
