使用 Paramiko SFTP 取得檔案的建立時間戳

WBOY
發布: 2024-02-09 09:20:16
轉載
864 人瀏覽過

使用 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 也是如此(在客戶端)。

因此在大多數情況下(即使您修補 Paramiko 以支援 SFTP 4),您將無法從 SFTP 伺服器檢索建立時間。

如果您有伺服器的 shell 存取權限,您也許能夠使用 shell 指令擷取建立時間。但這不再是 SFTP 問題。

以上是使用 Paramiko SFTP 取得檔案的建立時間戳的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!