我正在尝试使用 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
侃侃无极
不负相思意
炎炎设计
随时随地看视频慕课网APP
相关分类