python ftp.nlst() return empty list when where are (sub)directories on the server
python ftp.nlst() return empty list when where are (sub)directories on the server
My application needs to download all the directories from a remote FTP, I'm testing for the first time the python's ftplib
.
ftplib
When I try to list all the directories in the remote FTP with the command ftp.nlst()
it return an empty list. I know for sure that the directory is not empty because this command: ftp.retrlines('list')
returned an object displaying the names of the subfolders inside the directory.
ftp.nlst()
ftp.retrlines('list')
While I was testing I tried other commands like ftp.cwd('/other-dir/')
or ftp.pwd()
, but none of these seems to work.
ftp.cwd('/other-dir/')
ftp.pwd()
This is the code that I'm using to display the list of subdirectories:
from ftplib import FTP
def ftpConnection():
ftp = FTP('ftp-address')
ftp.login('user', 'password')
lista = ftp.nlst()
return (lista)
print(ftpConnection())
Output:
As you can see the list is empty.
This is my code for retrlines
:
retrlines
def ftpConnection():
ftp = FTP('remoteFtp')
ftp.login('user', 'password')
ftp.retrlines('LIST')
print (ftpConnection())
Output:
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 09:23 .
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 28 05:11 103367
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 02:01 121901
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Sep 23 2016 123233
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 09:19 125183
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 02:34 133028
This is the output from command-line ftp
:
ftp
230-Welcome clt_kantar_italy from remoteFtp. You are now logged in to the server.
230 User logged in, proceed.
ftp> dir
200 PORT command successful.
150 File status okay; about to open data connection.
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 09:23 .
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 28 05:11 103367
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 02:01 121901
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Sep 23 2016 123233
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 09:19 125183
drw-rw-rwx 1 clt_kantar_italy clt_kantar_italy 512 Jun 29 02:34 133028
226 Closing data connection. Transferred 481 bytes.
ftp: 484 bytes received in 0.01secondi 37.23Kbyte/sec)
ftp> ls
200 PORT command successful.
150 File status okay; about to open data connection.
226 Closing data connection. Transferred 0 bytes.
So by "local ftp", are you referring to a completely different FTP server? + Show us your code for
retrlines
and what does it return. + You still didn't explain the discrepancy in your problem description. + If you connect with command-line ftp
, what do you get for ls
and dir
commands?– Martin Prikryl
Jun 29 at 12:23
retrlines
ftp
ls
dir
Yes, with "local ftp" i mean a different FTP Server i'm gonna add the code to the question
– Gargantua
Jun 29 at 12:26
I've started thinking it's a firewall problem, could it be?
– Gargantua
Jun 29 at 12:32
Hardly. - Once again: If you connect with command-line
ftp
, what do you get for ls
and dir
(Windows ftp
) or nlist
(*nix ftp
) commands?– Martin Prikryl
Jun 29 at 12:34
ftp
ls
dir
ftp
nlist
ftp
1 Answer
1
So you see it yourself with ftp
(from the behavior I assume it's Windows ftp.exe
).
ftp
ftp.exe
dir
LIST
ls
NLST
So it's how your FTP server behaves - It does not return folders in NLST
.
NLST
If you need to retrieve folders from your FTP server, you have to use LIST
:
LIST
FTP.dir
FTP.retrlines('LIST')
Ok now it's clear, thank you for your patience.
– Gargantua
Jun 29 at 12:48
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
ok i'm going to edit the question
– Gargantua
Jun 29 at 12:05