你知道是否真的有办法从 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 问题。