python paramiko sftp,Python Paramiko SFTP获取文件以及文件时间戳/统计信息

# create SSHClient instance

ssh = paramiko.SSHClient()

list = []

# AutoAddPolicy automatically adding the hostname and new host key

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.load_system_host_keys()

ssh.connect(hostname, port, username, password)

stdin, stdout, stderr = ssh.exec_command("cd *path*; ls")

for i in stdout:

list.append(i)

sftp = ssh.open_sftp()

for i in list:

tempremote = ("*path*" + i).replace('\n', '')

templocal = ("*path*" + i).replace('\n', '')

try:

#Get the file from the remote server to local directory

sftp.get(tempremote, templocal)

except Exception as e:

print(e)

Remote Server File Date Modified Stat : 6/10/2018 10:00:17

Local File Date Modified Stat : Current datetime

But I found that the date modified changed after done copy the file.

Is there anyway to copy remote file along with the file stat to the local file too ?

解决方案

Paramiko indeed won't preserve timestamp when transferring files.

You have to explicitly call the os.utime after the download.

Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.get() method.

You can reuse their implementation (code simplified by me):

sftpattrs = sftp.stat(tempremote)

os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))

Similarly for uploads.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值