python - 使用 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 递归列出所有文件会更容易吗?
解决方案
由于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 应用程序)。
推荐阅读
- angular - Angular - 使用来自外部“资产”的翻译
- excel - Excel 公式:如何通过列名引用单元格
- c# - 如何在 VSCode 中为 nuget 包配置本地提要?
- python-3.x - python将字符串转换为三个块,如果不是两个块
- sql-server - SQL 函数 SYSDATETIME() 可以返回重复值吗?
- reactjs - 'npm run dev' vs 'next build' tsx 代码编译限制不同级别
- excel - 如何在excel中创建动态超链接?
- html - 创建相对于其自身宽度的响应式 html 元素
- mysql - 如何将表及其数据从一个数据库复制到另一个数据库?
- firebase - 有没有办法通过提供服务帐户凭据在一个函数中访问两个 Firestore 项目?