If you need to send a file to an SFTP server, you can easily do that with Python. In this post, I’ll show you how.
To write to an SFTP server, you’ll need to following information:
- The destination SFTP site
- If you need credentials to authenticate, you’ll need a username and a password
- A local path to your file
- A destination path where the file will be placed on the SFTP server
Once you have that, you can input those variables into the code below:
import paramiko import pandas #if you are converting a DataFrame to a CSV and uploading it host = "sftp_site" username = "username" password = "password" file_name = 'test.csv' df.to_csv(file_name) #remove this line if you aren't converting a DataFrame port = 22 transport = paramiko.Transport((host, port)) destination_path = "/"+file_name local_path = "/content/"+file_name #if using google colab, this will work with no modifications. Otherwise, overwrite with your local file path to the file transport.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(local_path, destination_path) sftp.close() transport.close()
Final Thoughts
Check out more Python tricks in this Colab Notebook or in my recent Python Posts.
Thanks for reading!