使用 Paramiko SFTP 获取文件的创建时间戳

WBOY
发布: 2024-02-09 09:20:16
转载
1349人浏览过

使用 paramiko sftp 获取文件的创建时间戳

问题内容

你知道是否真的有办法从 sftp 服务器获取“ctime”(文件创建的时间戳)吗?使用 paramiko 进行 sftp,我只看到“atime”和“mtime”。但是,我正在尝试访问文件的原始创建时间戳(而不是“atime”)。

这是我构建的当前代码,但注释掉了有关文件创建时间戳的部分,因为它会导致错误:

for file in tqdm(sftp.listdir()):
    # Debug check:
    print('We are now in the try loop:')
    
    # Look for files that have the same starting 25 characters as the column
    # in the mapper file:
    mask = mapper.file_name_startswith.str[:25].str.contains(file[:25])
    
    # Grab the destination path info from the mapper file:
    dest_path = mapper[mask]['destination_path'].values[0]

    # Get the timestamp of the original file before we remove it, for both modified & created:
    remote_mod_time = sftp.stat(file).st_mtime 
    # Need to use a different method to get the created date:
    '''
    remote_file_attrs = sftp.listdir_attr('.')  
    for attr in remote_file_attrs:  
        if attr.filename == file:  
            remote_create_time = attr.st_ctime  
            break  
    '''       
    # Move the current file to our desired local (destination) path:
    local_path = os.path.join(dest_path, file)
    sftp.get(file, local_path)

    # Set the modified date timestamp of the downloaded file to match the timestamp of the original     file:  
    os.utime(local_path, (remote_mod_time, remote_mod_time)) 

    # Set the created date (cannot use os.utime for this) to match the timestamp of the original file:
    #date_time = pywintypes.Time(remote_create_time)  
    #win32file.SetFileTime(local_path, date_time, None, None)  
    
    # Remove the current file, which is being processed, from the sftp server:
    #sftp.remove(file)
    
    # Append the file to the "done_file" list:
    done_files.append(file)
登录后复制

正确答案


仅从 SFTP 版本 4 开始支持文件创建时间。大多数 SFTP 服务器(尤其是 OpenSSH)仅支持 SFTP 版本 3。 Paramiko 也是如此(在客户端)。

Android开发教程与笔记pdf版
Android开发教程与笔记pdf版

Android文件存取与数据库编程知识,文件操作主要是读文件、写文件、读取静态文件等,同时还介绍了创建添加文件内容并保存,打开文件并显示内容;数据库编程方面主要介绍了SQLite数据库的使用、包括创建、删除、打开数据库、非查询SQL操作指令、查询SQL指令-游标Cursors等知识。

Android开发教程与笔记pdf版 17
查看详情 Android开发教程与笔记pdf版

因此在大多数情况下(即使您修补 Paramiko 以支持 SFTP 4),您将无法从 SFTP 服务器检索创建时间。

如果您具有服务器的 shell 访问权限,您也许能够使用 shell 命令检索创建时间。但这不再是 SFTP 问题。

以上就是使用 Paramiko SFTP 获取文件的创建时间戳的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号