python - 使用 pysftp 优化检索文件大小的性能
问题描述
我需要获取某些位置(在系统和 SFTP 内)的文件详细信息,并获取 SFTP 上某些位置的文件大小,这可以使用共享代码来实现。
def getFileDetails(location: str):
filenames: list = []
if location.find(":") != -1:
for file in glob.glob(location):
filenames.append(getFileNameFromFilePath(file))
else:
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
remote_files = [x.filename for x in sorted(sftp.listdir_attr(location), key=lambda f: f.st_mtime)]
if location == LOCATION_SFTP_A:
for filename in remote_files:
filenames.append(filename)
sftp_archive_d_size_mapping[filename] = sftp.stat(location + "/" + filename).st_size
elif location == LOCATION_SFTP_B:
for filename in remote_files:
filenames.append(filename)
sftp_archive_e_size_mapping[filename] = sftp.stat(location + "/" + filename).st_size
else:
for filename in remote_files:
filenames.append(filename)
sftp.close()
return filenames
LOCATION_SFTP_A和LOCATION_SFTP_B中有超过10000个文件。对于每个文件,我需要获取文件大小。获得我正在使用的尺寸
sftp_archive_d_size_mapping[filename] = sftp.stat(location + "/" + filename).st_size
sftp_archive_e_size_mapping[filename] = sftp.stat(location + "/" + filename).st_size
# Time Taken : 5 min+
sftp_archive_d_size_mapping[filename] = 1 #sftp.stat(location + "/" + filename).st_size
sftp_archive_e_size_mapping[filename] = 1 #sftp.stat(location + "/" + filename).st_size
# Time Taken : 20-30 s
如果我注释sftp.stat(location + "/" + filename).st_size
并分配静态值运行整个代码只需要 20-30 秒。我正在寻找一种如何优化时间并获取文件大小详细信息的方法。
解决方案
推荐阅读
- python - 检查日期熊猫的差异并只保留某些ID
- c# - C# .NET Core 3 API 模型验证忽略属性
- git - 为什么合并会覆盖对以下文件的本地更改?
- embedded - 在 STM32L412 上使用 DMA/i2c
- javascript - 如果是 OS / iphone,则风格不同
- javascript - 如何在 if ... else 语句中使用 setTimeOut
- ios - Cordova 插件目标 C,FMDatabase tableExists 参数运行时出错
- java - 在java中,删除文件后使用inputstream读取文件
- flutter - Flutter parse-server sdk 无法删除对象
- python - pytest 中缺少参数化变量