根据文件名中的时间戳从SFTP服务器下载最新的文件
Download the latest file according to timestamp in file name from SFTP server
我正在尝试获取远程 Linux 服务器目录中的最新文件。 SFTP 服务器中的文件每 4 小时创建一次,文件的特定名称以 filegen_date_hour.json
开头,如下例所示。在这种情况下,最新文件 'filegen_20200101_0800.json' 需要传输到我的本地目录。
filegen_20200101_0000.json
filegen_20200101_0400.json
filegen_20200101_0800.json
我使用下面的 Python 3 代码,但出现错误
latestFile = max(listFile, key=os.path.getctime)
ValueError: max() arg is an empty sequence
下面的 SFTP 代码
myHostname = "192.168.100.10"
myUsername = "user"
myPassword = "password"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
with sftp.cd('/home/operation/genfiles/'):
fileDir = '/home/operation/genfiles/filegen_*.json'
**#file have specific pattern with filegen_*.json**
listFile = glob.glob(fileDir)
latestFile = max(listFile, key=os.path.getctime)
sftp.get(latestFile)
感谢对此事的帮助。感谢您的回复和帮助。
首先,您不能使用 glob
列出 SFTP 服务器上的文件。 glob
不会神奇地开始查询 SFTP 服务器只是因为您之前已经打开了 SFTP 连接。它仍然会查询本地文件系统。
使用 pysftp Connection.listdir
。虽然它不支持通配符,但您必须在本地过滤您想要的文件。喜欢这里:
只有这样你才能尝试找到最新的文件。
一般情况下,您可以使用文件修改时间,如下所示:
代码是针对Paramiko SFTPClient.listdir_attr
,但它与pysftp Connection.listdir_attr
.
相同
但是对于你的情况,我不确定你是否可以依赖修改时间戳。看来您实际上想在文件名中使用时间戳。使用您的文件名格式,您可以简单地按字典顺序选择最后一个文件。
import fnmatch
...
with sftp.cd('/home/operation/genfiles'):
files = []
for filename in sftp.listdir():
if fnmatch.fnmatch(filename, "filegen_*.json"):
files.append(filename)
latestFile = max(files)
强制警告:不要设置cnopts.hostkeys = None
,除非你不关心安全。有关正确的解决方案,请参阅 Verify host key with pysftp.
我正在尝试获取远程 Linux 服务器目录中的最新文件。 SFTP 服务器中的文件每 4 小时创建一次,文件的特定名称以 filegen_date_hour.json
开头,如下例所示。在这种情况下,最新文件 'filegen_20200101_0800.json' 需要传输到我的本地目录。
filegen_20200101_0000.json
filegen_20200101_0400.json
filegen_20200101_0800.json
我使用下面的 Python 3 代码,但出现错误
latestFile = max(listFile, key=os.path.getctime)
ValueError: max() arg is an empty sequence
下面的 SFTP 代码
myHostname = "192.168.100.10"
myUsername = "user"
myPassword = "password"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
with sftp.cd('/home/operation/genfiles/'):
fileDir = '/home/operation/genfiles/filegen_*.json'
**#file have specific pattern with filegen_*.json**
listFile = glob.glob(fileDir)
latestFile = max(listFile, key=os.path.getctime)
sftp.get(latestFile)
感谢对此事的帮助。感谢您的回复和帮助。
首先,您不能使用 glob
列出 SFTP 服务器上的文件。 glob
不会神奇地开始查询 SFTP 服务器只是因为您之前已经打开了 SFTP 连接。它仍然会查询本地文件系统。
使用 pysftp Connection.listdir
。虽然它不支持通配符,但您必须在本地过滤您想要的文件。喜欢这里:
只有这样你才能尝试找到最新的文件。
一般情况下,您可以使用文件修改时间,如下所示:
代码是针对Paramiko SFTPClient.listdir_attr
,但它与pysftp Connection.listdir_attr
.
但是对于你的情况,我不确定你是否可以依赖修改时间戳。看来您实际上想在文件名中使用时间戳。使用您的文件名格式,您可以简单地按字典顺序选择最后一个文件。
import fnmatch
...
with sftp.cd('/home/operation/genfiles'):
files = []
for filename in sftp.listdir():
if fnmatch.fnmatch(filename, "filegen_*.json"):
files.append(filename)
latestFile = max(files)
强制警告:不要设置cnopts.hostkeys = None
,除非你不关心安全。有关正确的解决方案,请参阅 Verify host key with pysftp.