如何使用Paramiko仅从SFTP服务器下载最新文件? [英] How to download only the latest file from SFTP server with Paramiko?

查看:74
本文介绍了如何使用Paramiko仅从SFTP服务器下载最新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写脚本,连接到我大学的 SFTP 服务器并下载带有练习的最新文件.到目前为止,我已经对 Paramiko 示例中的代码进行了一些更改,但我不知道如何下载最新的文件.

这是我的代码:

import functools进口paramiko类 AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):def missing_host_key(self, client, hostname, key):返回地址 = '地址'用户名 = '用户名'密码 = '密码'客户端 = paramiko.SSHClient()client.set_missing_host_key_policy(AllowAnythingPolicy())客户端连接(地址,用户名=用户名,密码=密码)def my_callback(filename, bytes_so_far, bytes_total):打印('%r 的传输正在进行' % 文件名)sftp = client.open_sftp()sftp.chdir('/目录/到/文件')对于 sorted(sftp.listdir()) 中的文件名:如果 filename.startswith('Temat'):callback_for_filename = functools.partial(my_callback, 文件名)sftp.get(文件名,文件名,回调=callback_for_filename)客户端关闭()

解决方案

使用 SFTPClient.listdir_attr 而不是 SFTPClient.listdir 以获取具有属性(包括文件时间戳)的列表.>

然后,找到一个.st_mtime 属性.

代码如下:

最新 = 0最新文件 = 无对于 sftp.listdir_attr() 中的 fileattr:如果 fileattr.filename.startswith('Temat') 和 fileattr.st_mtime >最新的:最新 = fileattr.st_mtime最新文件 = 文件属性.文件名如果 latestfile 不是 None:sftp.get(最新文件,最新文件)

<小时>

有关更复杂的示例,请参阅如何获取包含 Linux 中感兴趣的特定文件的最新文件夹并使用以下命令下载该文件Python 中的 Paramiko?

I want to write script that connects to my university SFTP server and downloads the latest file with exercises. So far I've changed a little bit the code from Paramiko example, but I do not know how to download the latest file.

Here is my code :

import functools
import paramiko 

class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return

adress = 'adress'
username = 'username'
password = 'password'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(AllowAnythingPolicy())
client.connect(adress, username= username, password=password)

def my_callback(filename, bytes_so_far, bytes_total):
    print ('Transfer of %r is in progress' % filename) 

sftp = client.open_sftp()
sftp.chdir('/directory/to/file')
for filename in sorted(sftp.listdir()):
    if filename.startswith('Temat'):
        callback_for_filename = functools.partial(my_callback, filename)
        sftp.get(filename, filename, callback=callback_for_filename)

client.close() 

解决方案

Use the SFTPClient.listdir_attr instead of the SFTPClient.listdir to get listing with attributes (including the file timestamp).

Then, find a file entry with the greatest .st_mtime attribute.

The code would be like:

latest = 0
latestfile = None

for fileattr in sftp.listdir_attr():
    if fileattr.filename.startswith('Temat') and fileattr.st_mtime > latest:
        latest = fileattr.st_mtime
        latestfile = fileattr.filename

if latestfile is not None:
    sftp.get(latestfile, latestfile)


For a more complex example, see How to get the latest folder that contains a specific file of interest in Linux and download that file using Paramiko in Python?

这篇关于如何使用Paramiko仅从SFTP服务器下载最新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆