Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ftplib: How to store results of `FTP.retrlines` in a list?

Tags:

python

ftp

ftplib

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 ?

like image 736
Tao Xiao Avatar asked Sep 06 '15 05:09

Tao Xiao


2 Answers

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.

like image 157
Martin Prikryl Avatar answered Nov 15 '22 14:11

Martin Prikryl


You can use FTP.nlst() method. It returns the file names as a list.

>>> FTP.nlst('path')
['x','y','z']
like image 23
Vishesh Shrivastav Avatar answered Nov 15 '22 16:11

Vishesh Shrivastav