Paramiko SFTP를 사용하여 파일 생성 타임스탬프 가져오기

WBOY
풀어 주다: 2024-02-09 09:20:16
앞으로
865명이 탐색했습니다.

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

질문 내용

SFTP 서버에서 "ctime"(파일 생성 타임스탬프)을 얻을 수 있는 방법이 실제로 있는지 아시나요? sftp에 paramiko를 사용하면 "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(클라이언트 측)도 마찬가지입니다.

따라서 대부분의 경우(SFTP 4를 지원하도록 Paramiko를 패치하더라도) SFTP 서버에서 생성 시간을 검색할 수 없습니다.

서버에 대한 셸 액세스 권한이 있는 경우 셸 명령을 사용하여 생성 시간을 검색할 수 있습니다. 그러나 이는 더 이상 SFTP 문제가 아닙니다.

위 내용은 Paramiko SFTP를 사용하여 파일 생성 타임스탬프 가져오기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:stackoverflow.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!