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

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

【问题讨论】:

    标签: python csv pysftp


    【解决方案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
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 2016-07-07
      • 2016-01-28
      • 1970-01-01
      • 2015-04-06
      • 2022-11-10
      相关资源
      最近更新 更多