Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating list from retrlines in Python

Tags:

python

ftp

ftplib

How exactly would you create a list of the entries in an FTP directory?

This is my code so far:

import ftplib

files = []

my_ftp = ftplib.FTP(HOST)
my_ftp.login(USERNAME,PASSWORD)

line = my_ftp.retrlines("NLST",files.append(line))
my_ftp.quit()

The error says that the variable line is being used before it is defined.

like image 899
Abram I Avatar asked Apr 01 '12 23:04

Abram I


2 Answers

You probably just want to use nlst:

>>> my_ftp.nlst()
['pub', 'etc', 'ports']
like image 200
zigg Avatar answered Nov 06 '22 22:11

zigg


A little change to the callback argument and the following should work

line = my_ftp.retrlines("NLST",files.append)
like image 5
Milo Wielondek Avatar answered Nov 06 '22 22:11

Milo Wielondek