I’m working on uploading a file to a server via SFTP. The given put method of the SFTP client expects as first argument the relative or absolute local path of the file I want to upload and as second argument the remote path where the file should be uploaded:
localFilePath = 'C:/Users/user/Output.csv'
remoteFilePath = '/remote/Output.csv'
sftp.put(localFilePath, remoteFilePath)
How is it possible to customize the naming of the file in the remoteFilePath by adding the actual datetime so it should look like this : Output_2021-12-20T16:27:28Z.csv ?
>Solution :
You can format the remoteFilePath with the current datetime:
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