我在 Python 3.3 中有以下代码:
ftpHost="myhost"
link=ftplib.FTP(ftpHost)
link.login(user="me",passwd="alsome")
t=link.nlst()
link.quit()
print(t)
我得到的输出是
['.','..','dir1','dir2']
在 Python 3 文档中,写到 FTP.nlst “自 3.3 版以来已弃用:改用 mlsd()”。当我尝试
ftpHost="myhost"
link=ftplib.FTP(ftpHost)
link.login(user="me",passwd="alsome")
t=link.mlsd()
link.quit()
print(t)
我得到的输出是
<generator object mlsd at 0x0000000002A0F120>
这不是想要的答案。我不知道在哪里可以得到目录列表。如何使用 mlsd 获取目录列表?
如果我尝试@fourtheye 的建议并更改为 t=list(link.mlsd)
我得到的错误是
Traceback (most recent call last):
File "C:/xyz/python-test/python-test.py", line 17, in <module>
main()
File "C:/xyz/python-test/python-test.py", line 12, in main
t=list(link.mlsd())
File "C:\WinPython-64bit-3.3.2.1\python-3.3.2.amd64\lib\ftplib.py", line 556, in mlsd
self.retrlines(cmd, lines.append)
File "C:\WinPython-64bit-3.3.2.1\python-3.3.2.amd64\lib\ftplib.py", line 446, in retrlines
with self.transfercmd(cmd) as conn, \
File "C:\WinPython-64bit-3.3.2.1\python-3.3.2.amd64\lib\ftplib.py", line 386, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\WinPython-64bit-3.3.2.1\python-3.3.2.amd64\lib\ftplib.py", line 352, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\WinPython-64bit-3.3.2.1\python-3.3.2.amd64\lib\ftplib.py", line 259, in sendcmd
return self.getresp()
File "C:\WinPython-64bit-3.3.2.1\python-3.3.2.amd64\lib\ftplib.py", line 233, in getresp
raise error_perm(resp)
ftplib.error_perm: 500 MLSD not understood