>  기사  >  백엔드 개발  >  Paramiko SFTP를 사용하여 파일 생성 타임스탬프 가져오기

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

WBOY
WBOY앞으로
2024-02-09 09:20:16868검색

使用 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으로 문의하시기 바랍니다. 삭제