您是否知道是否确实有办法获取“ctime”(文件从 SFTP 服务器创建的时间戳)?使用 Paramiko 进行 SFTP,我只看到“atime”和“mtime”。但是,我正在尝试访问文件的原始创建时间戳(而不是“atime”)。
这是我构建的当前代码,但注释掉了有关文件创建时间戳的部分,因为它会导致错误:
for file in tqdm(sftp.listdir()):
# Create a counter to see the loops:
i = 1
try:
# 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),您将无法从 SFTP 服务器检索创建时间。