通过 SFTP 上传到服务器时的自定义文件名 [重复]

Posted

技术标签:

【中文标题】通过 SFTP 上传到服务器时的自定义文件名 [重复]【英文标题】:Custom file name while uploading to your server via SFTP [duplicate] 【发布时间】:2022-01-22 05:00:19 【问题描述】:

我正在通过 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

【问题讨论】:

【参考方案1】:

您可以使用当前日期时间格式化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

【讨论】:

以上是关于通过 SFTP 上传到服务器时的自定义文件名 [重复]的主要内容,如果未能解决你的问题,请参考以下文章