[英]Optimize the performance of retreiving file sizes with 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 秒。 我正在寻找一种方法如何优化时间并获取文件大小的详细信息。
Connection.listdir_attr
已经在SFTPAttributes.st_size
为您提供了文件大小。
无需为每个文件调用Connection.stat
来获取大小(再次)。
也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.