正在从中检索用于文件列表的生成器对象的数据FTP.mlsd文件()

2024-04-29 12:09:42 发布

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

Python documentation for FTP.mlsd()表示它返回生成元组的生成器对象。运行以下代码后,我就得到了该对象的地址。在这一点上,我假设我已经成功地从我的服务器收到了列表,因为它没有弹出任何错误(如果假设是错误的,请纠正我)。在

this question的回答建议我应该使用next()来获取对象中的值。但是,这样做时,它会显示错误消息ftplib.error_perm: 501 Option not understood.

Code

from ftplib import FTP
ftp = FTP('192.168.0.104')
ftp.login('testing','testing')
ftp.cwd('FTP_Test_Site')

temp = ftp.mlsd(path="", facts=["type", "size", "perm"])
print(temp)
print(next(temp))

ftp.quit()

Output

^{pr2}$

你知道我哪里出错了吗?在


Tags: 对象代码for地址documentation错误ftptesting
1条回答
网友
1楼 · 发布于 2024-04-29 12:09:42

ftp mlsd是一个生成器。所以你必须循环检查mlsd。在

例如:

dirs=[]
nondirs=[]
for item in ftp.mlsd(dir):
    if item[1]['type'] == 'dir':
        dirs.append(item[0])
    else:
        nondirs.append(item[0])

相关问题 更多 >