summaryrefslogtreecommitdiffstats
path: root/Lib/ftplib.py
diff options
context:
space:
mode:
authorGiampaolo Rodola' <g.rodola@gmail.com>2011-05-06 17:49:08 (GMT)
committerGiampaolo Rodola' <g.rodola@gmail.com>2011-05-06 17:49:08 (GMT)
commitd78def9474ceac79576e0b6382b15c90cc54aff8 (patch)
tree5d2ef2ef8822ffd6660099dfb068578464a87875 /Lib/ftplib.py
parent0872816dc1fbe0bf3b126874a31afd8e37677ae3 (diff)
downloadcpython-d78def9474ceac79576e0b6382b15c90cc54aff8.zip
cpython-d78def9474ceac79576e0b6382b15c90cc54aff8.tar.gz
cpython-d78def9474ceac79576e0b6382b15c90cc54aff8.tar.bz2
Issue #11072: added MLSD command (RFC-3659) support to ftplib.
Diffstat (limited to 'Lib/ftplib.py')
-rw-r--r--Lib/ftplib.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index e836b72..752e2b4 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -426,7 +426,7 @@ class FTP:
"""Retrieve data in line mode. A new port is created for you.
Args:
- cmd: A RETR, LIST, NLST, or MLSD command.
+ cmd: A RETR, LIST, or NLST command.
callback: An optional single parameter callable that is called
for each line with the trailing CRLF stripped.
[default: print_line()]
@@ -527,6 +527,34 @@ class FTP:
cmd = cmd + (' ' + arg)
self.retrlines(cmd, func)
+ def mlsd(self, path="", facts=[]):
+ '''List a directory in a standardized format by using MLSD
+ command (RFC-3659). If path is omitted the current directory
+ is assumed. "facts" is a list of strings representing the type
+ of information desired (e.g. ["type", "size", "perm"]).
+
+ Return a generator object yielding a tuple of two elements
+ for every file found in path.
+ First element is the file name, the second one is a dictionary
+ including a variable number of "facts" depending on the server
+ and whether "facts" argument has been provided.
+ '''
+ if facts:
+ self.sendcmd("OPTS MLST " + ";".join(facts) + ";")
+ if path:
+ cmd = "MLSD %s" % path
+ else:
+ cmd = "MLSD"
+ lines = []
+ self.retrlines(cmd, lines.append)
+ for line in lines:
+ facts_found, _, name = line.rstrip(CRLF).partition(' ')
+ entry = {}
+ for fact in facts_found[:-1].split(";"):
+ key, _, value = fact.partition("=")
+ entry[key.lower()] = value
+ yield (name, entry)
+
def rename(self, fromname, toname):
'''Rename a file.'''
resp = self.sendcmd('RNFR ' + fromname)