通过 SFTP 上传到您的服务器时自定义文件名

Custom file name while uploading to your server via SFTP

我正在努力通过 SFTP 将文件上传到服务器。 SFTP 客户端的给定 put 方法期望将我要上传的文件的相对或绝对本地路径作为第一个参数,并将文件应上传到的远程路径作为第二个参数:

localFilePath = 'C:/Users/user/Output.csv' 
remoteFilePath = '/remote/Output.csv' 
sftp.put(localFilePath, remoteFilePath)

如何通过添加实际日期时间来自定义 remoteFilePath 中文件的命名,使其看起来像这样:Output_2021-12-20T16:27:28Z.csv ?

您可以使用当前日期时间格式化 remoteFilePath

from datetime import datetime

now = datetime.now()

remoteFilePath = f'/remote/Output_{now.isoformat()}.csv' # /remote/Output_2021-12-20T12:39:39.385804.csv

# Or you can use `strftime` method to set the 'Z' at the end

remoteFilePath2 = f"/remote/Output_{now.strftime('%Y-%m-%dT%H:%M:%SZ')}}.csv" # /remote/Output_2021-12-20T12:40:25Z.csv