通过 FTP 确定列表是否是 Python 中的目录或文件

Determine if a listing is a directory or file in Python over FTP(通过 FTP 确定列表是否是 Python 中的目录或文件)
本文介绍了通过 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 中的目录或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)