pythonftp.nlst文件()当s上的(子)目录在何处时返回空列表

2024-10-01 02:39:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我的应用程序需要从远程FTP下载所有目录,我是第一次测试python的ftplib。在

当我试图用命令ftp.nlst()列出远程FTP中的所有目录时,它返回一个空列表。我确信目录不是空的,因为这个命令:ftp.retrlines('list')返回了一个显示目录内子文件夹名称的对象。在

其他的cd5似乎都不起作用。在

这是我用来显示子目录列表的代码:

from ftplib import FTP

def ftpConnection():
    ftp = FTP('ftp-address')
    ftp.login('user', 'password') 
    lista = ftp.nlst()
    return (lista)

print(ftpConnection())

输出:

^{pr2}$

如你所见,列表是空的。在


这是我的retrlines代码:

def ftpConnection():
    ftp = FTP('remoteFtp')
    ftp.login('user', 'password') 
    ftp.retrlines('LIST')

print (ftpConnection())

输出:

drw-rw-rwx 1          512 Jun 29 09:23 .
drw-rw-rwx 1          512 Jun 28 05:11 103367
drw-rw-rwx 1          512 Jun 29 02:01 121901
drw-rw-rwx 1          512 Sep 23  2016 123233
drw-rw-rwx 1          512 Jun 29 09:19 125183
drw-rw-rwx 1          512 Jun 29 02:34 133028

这是命令行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          512 Jun 29 09:23 .
drw-rw-rwx 1         512 Jun 28 05:11 103367
drw-rw-rwx 1          512 Jun 29 02:01 121901
drw-rw-rwx 1          512 Sep 23  2016 123233
drw-rw-rwx 1          512 Jun 29 09:19 125183
drw-rw-rwx 1          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.

Tags: toin目录列表databytesftpconnection
1条回答
网友
1楼 · 发布于 2024-10-01 02:39:46

因此,您可以通过ftp看到它(根据我假设它是Windows的行为ftp.exe)。在

  • dir命令使用LISTFTP命令,返回文件夹
  • ls命令使用NLSTFTP命令,不返回文件夹。在

所以这就是您的FTP服务器的行为方式–它不返回NLST中的文件夹。在

如果需要从FTP服务器检索文件夹,则必须使用LIST

相关问题 更多 >