在python的标准库ftplib
中,FTP类提供了一个dir()
函数,可以检索出当前路径下文件列表和这些文件有关的信息并打印出来,但是这个函数并没有返回值,只有打印的功能
FTP类还提供另一个nlst()
函数,与dir()
函数类似,可以检索出文件名列表并为每一行调用一个回调函数,默认回调函数会将该行打印到sys.stdout
,但是这些列表并不包括目录,也没有完整的详细信息
想要获取FTP目录下所有内容并返回,想了半天,还是决定从源码下手
dir()
源码:
def dir(self, *args):
'''List a directory in long form.
By default list current directory to stdout.
Optional last argument is callback function; all
non-empty arguments before it are concatenated to the
LIST command. (This *should* only be used for a pathname.)'''
cmd = 'LIST'
func = None
if args[-1:] and type(args[-1]) != type(''):
args, func = args[:-1], args[-1]
for arg in args:
if ar