Python ftplib:如何在列表中存储`FTP.retrlines`的结果? [英] Python ftplib: How to store results of `FTP.retrlines` in a list?

查看:1636
本文介绍了Python ftplib:如何在列表中存储`FTP.retrlines`的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索一个目录的文件名,并使用方法 ftplib.retrlines('NLST'+ path)



它打印目录路径中的所有文件名。但我想将这些文件的名称存储在容器中,例如列表中,而不是将它们打印在控制台中。怎么做 ? FTP.retrlines 是一个回调< code $。

  FTP.retrlines(command [,callback])




$ b

行= []
sess.retrlines('NLST'+ path,lines.append)

另请参阅在Python中使用retrlines创建列表


I'd like to retrieve files' name of a directory and I use the method ftplib.retrlines('NLST' + path).

It prints all files' names in the directory path. But I want to store those files' names in a container, e.g., a list, instead of printing them in the console. How to do that ?

解决方案

The second (optional) argument to the FTP.retrlines is a callback.

FTP.retrlines(command[, callback])

You can use it like:

lines = []
sess.retrlines('NLST ' + path, lines.append)

See also Creating list from retrlines in Python.

这篇关于Python ftplib:如何在列表中存储`FTP.retrlines`的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆