由于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 应用程序)。