我的应用程序需要从远程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.
因此,您可以通过
ftp
看到它(根据我假设它是Windows的行为ftp.exe
)。在dir
命令使用LIST
FTP命令,返回文件夹ls
命令使用NLST
FTP命令,不返回文件夹。在所以这就是您的FTP服务器的行为方式–它不返回
NLST
中的文件夹。在如果需要从FTP服务器检索文件夹,则必须使用
LIST
:相关问题 更多 >
编程相关推荐