Something I Learned This Week — Paramiko’s Remote File Management
Another Python package to the rescue…
A couple of days ago, I was busy at my whiteboard going over the design of a new Python application that would be used to read files on my NAS (Network Attached Storage). Since it’s a fairly simple app, there was only one question that was really bugging me. How do I get Python to read a file on a remote server? Little did I know that the answer was already in front of me. On multiple past projects, I have used a package called Paramiko. According to the package website, Paramiko is a Python implementation of the SSH (Secure Shell) protocol. If you do what I did and don’t read the documentation, you’ll also discover that it can be an SFTP (Simple File Transfer Protocol) client as well. With this newfound information, I figured that Paramiko would once again answer my question. But I needed to test it first.
Reading a Remote File
Eager to see if Paramiko will save the day once more, I created a new file called remote_operations.py.
touch remote_operations.pyOnce the new Python file was created, I opened it, imported Paramiko, and declared a new class:
import paramikoclass remote_operations:
def __init__(self):
passAfter that, I wrote a function that starts a new SSH connection:
def connect(self, hostname, username, password):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(hostname, username=username, password=password)
return clientThe next function I wrote will start an SFTP client and open the passed file:
def open_remote_file(self, ssh_client, filename):
sftp_client = ssh_client.open_sftp()
file = sftp_client.open(filename)
return fileEssentially, the open_remote_file function is the meat and potatoes of what I needed for my application to work properly. The last step to do before claiming success is to test these new functions. In order to do so, I created another file called test.py and imported the new remote_operations class within it.
from remote_operations import remote_operationsNext, I declared a new instance of the remote_operations class, called the connect function to open a new SSH session, and called the open_remote_file function.
test = remote_operations()client = test.connect("HOSTNAME", "USERNAME", "PASSWORD")file = test.open_remote_file(client, "/filepath/test.txt")Lastly, I wrote some test code to loop through the lines in the file and closed the SFTP client and SSH session.
for line in file:
print(line)file.close()
client.close()
Sending a File For Fun
While reading the SFTP documentation, I noticed that there was a function to send a file to a remote destination. Since being able to read a remote file went pretty easily, I decided to give this put function a shot. Going off what was already in my remote_operations class, I added a new function that when copies a file to the given remote destination.
Note: The put function is only for copying local files to a remote server.
def send_file(self, ssh_client, filename, destination):
sftp_client = ssh_client.open_sftp()
sftp_client.put(filename, destination)
return sftp_clientJust like when reading a file, it was necessary to check if the new send_file function will actually work. Using the already created test file, I made a call to the new function.
test = remote_operations()client = test.connect("HOSTNAME", "USERNAME", "PASSWORD")send = test.send_file(client, "test2.txt", "/destination/test2.txt")send.close()
client.close()
Other Goodies
Outside of reading files and sending them to remote locations, the Paramikos SFTP client has many more functions that can be used for managing files. I won’t go into great detail about them here, but some notable ones are:
- listdir — list all items in a specified directory
- mkdir — create a new directory at a specified path
- remove — removes a specified file (only works for files)
- rmdir — removes the given directory
- rename — changes the name of a file or directory
Feel free to play around with these and see what you can get them to do!
Conclusion
To say that Paramiko is an awesome package is a clear understatement. Its many features are easy to use and can help you get something up and running very quickly. In all honesty, I was under the impression that working with remote files was going to be a programming nightmare. Fortunately, I was proven wrong by how easy it is to use Paramikos functions. Let me know in the comments how you manage remote files and if you prefer a different Python package. Until next time, cheers!
Read all my articles for free with my weekly newsletter, thanks!
Want to read all articles on Medium? Become a Medium member today!
Check out some of my recent articles:
References:
Further Reading
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.






