Creating list from retrlines in Python

2019-01-19 07:51发布

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.

2条回答
狗以群分
2楼-- · 2019-01-19 08:16

You probably just want to use nlst:

>>> my_ftp.nlst()
['pub', 'etc', 'ports']
查看更多
贼婆χ
3楼-- · 2019-01-19 08:25

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

line = my_ftp.retrlines("NLST",files.append)
查看更多
登录 后发表回答