Paramiko SFTP を使用してファイルの作成タイムスタンプを取得する

WBOY
リリース: 2024-02-09 09:20:16
転載
821 人が閲覧しました

使用 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 中国語 Web サイトの他の関連記事を参照してください。

ソース:stackoverflow.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!