gpt4 book ai didi

python - 获取使用 pysftp Connection.walktree 迭代的文件和文件夹的属性?

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:10 26 4
gpt4 key购买 nike

我正在尝试使用 pysftp 在目录下递归地列出文件及其修改时间。问题是,它显示文件不存在于所有文件中,但不存在于目录中。

这是我的代码:

class SftpOps:
def __init__(self):
config = ConfigManager()
host = config.getSftpUrl()
port = config.getSftpPort()
user = config.getSftpUsername()
password = config.getSftpPassword()
self.source = config.getSourceDirRelativePath()

cnopts = pysftp.CnOpts()
cnopts.hostkeys.load('host_key')

self.sftp = pysftp.Connection(
host, port=port, username=user, password=password, cnopts=cnopts)

def downloadSourceDir(self):
print(self.sftp.listdir())
self.sftp.walktree(self.source, self.fileModifiedTimeCheck,
self.dirModifiedTimeCheck, self.fileModifiedTimeCheck, recurse=True)
self.sftp.close()

def fileModifiedTimeCheck(self, filepath):
filepath = os.path.join(self.source, filepath)
try:
for attr in self.sftp.listdir_attr(filepath):
print(f"{filepath}: {attr.st_atime}")
except FileNotFoundError as err:
print(f"No file at: {filepath}, failed with err: {err}")
except OSError as err:
print("OS error: {0}".format(err))
except:
print("Unexpected error:", sys.exc_info()[0])
raise

def dirModifiedTimeCheck(self, filepath):
filepath = os.path.join(self.source, filepath)
try:
for attr in self.sftp.listdir_attr(filepath):
print(f"{filepath}: {attr.st_atime}")
filepath = "tmp/"+filepath
except FileNotFoundError:
print(f"No dir at: {filepath}")
except OSError as err:
print("OS error: {0}".format(err))
except:
print("Unexpected error:", sys.exc_info()[0])
raise


# class LogOps:
# def __init__(self):


# class EmailOps:
# def __init__(self):

print("=========================================")
test_obj = SftpOps()
test_obj.downloadSourceDir()
print("=========================================")

当我为具有以下结构的目录尝试此操作时 enter image description here

它给出的错误如下:

=========================================
['.DS_Store', 'about-me.html', 'favicon.ico', 'index.html', 'test']
No file at: /Downloads/techtuft/.DS_Store, failed with err: [Errno 2] No such file
No file at: /Downloads/techtuft/about-me.html, failed with err: [Errno 2] No such file
No file at: /Downloads/techtuft/favicon.ico, failed with err: [Errno 2] No such file
No file at: /Downloads/techtuft/index.html, failed with err: [Errno 2] No such file
/Downloads/techtuft/test: 1569165379
No file at: /Downloads/techtuft/test/style.css, failed with err: [Errno 2] No such file
=========================================

请注意,它如何不显示目录“test”的错误。

最佳答案

您不能使用 Connection.listdir_attr用于文件。

你应该使用 Connection.stat .

尽管那无论如何都是非常低效的。您最好复制 walktree 函数的实现,并使其调用 Connection.listdir_attr 而不是 Connection.listdir。通过这种方式,您可以在对服务器的一次调用中获取目录中所有文件的时间戳,而不是低效地逐个文件地检索它们。

另见 Python SFTP download files older than x and delete networked storage .

关于python - 获取使用 pysftp Connection.walktree 迭代的文件和文件夹的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com