Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Custom file name while uploading to your server via SFTP

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading