首页 > 解决方案 > 使用 FTP 的递归文件列表

问题描述

我知道如何从 FTP 服务器列出目录中的所有文件:

import ftplib
ftp = ftplib.FTP()
ftp.connect("192.168.1.18", port=2240)
ftp.login()
ftp.cwd('path/to')
for f in ftp.mlsd():
    print(f)

但是获取递归文件列表(即子目录、子子目录等中的文件)的最佳方法是什么?

即 Python 3 的 FTP 的等价物,glob.glob('path/to/**/*', recursive=True)它递归地列出所有文件。

我可以通过手动输入每个目录,然后重做一个来做到这一点,msld()但我担心这会很慢(据我所知,在 FTP 中列出文件已经很慢了),所以这不是最佳选择。

使用 SFTP 如何做到这一点?使用 SFTP 递归列出所有文件会更容易吗?

标签: pythonftpftplib

解决方案


由于LIST -R, NLST -R,MLSD -R不适合我,我遵循@MartinPrikryl 的建议,这是一个 FTP 解决方案:

import ftplib, time
def list_recursive(ftp, remotedir):
    ftp.cwd(remotedir)
    for entry in ftp.mlsd():
        if entry[1]['type'] == 'dir':
            remotepath = remotedir + "/" + entry[0]
            print(time.time() - t0, remotepath)
            list_recursive(ftp, remotepath)
        else:
            print(entry)
ftp = ftplib.FTP()
ftp.connect("192.168.1.18", port=2240)
ftp.login()
t0 = time.time()
list_recursive(ftp, '/sdcard/music')

大约 900 个文件夹中的大约 20k 个文件花费了 344 秒(我的 FTP 服务器在手机上:cx File Explorer 应用程序)。


作为比较,这里有一个 SFTP 的解决方案:

import pysftp
def list_recursive(sftp, remotedir):
    for entry in sftp.listdir_attr(remotedir):
        remotepath = remotedir + "/" + entry.filename
        if sftp.isdir(remotepath):
            print(remotepath)            
            list_recursive(sftp, remotepath)
        else:
            print(entry.st_size, entry.st_mtime, entry.filename)
cnopts = pysftp.CnOpts()  # for local testing
cnopts.hostkeys = None 
with pysftp.Connection('192.168.1.18', port=2222, username='ssh', password='', cnopts=cnopts) as sftp:
    list_recursive(sftp, 'music')

大约 900 个文件夹中的大约 20k 个文件花费了 222 秒(我在 Android 手机上使用了 SSH/SFTP Server 应用程序)。


推荐阅读