Python provides various libraries to handle various types of tasks. One such library is
pysftp
, a simple interface for SFTP sessions (i.e., Secure File Transfer Protocol) in Python. The library wraps
paramiko’s
SFTP functionality for easier usage and convenience. This article will illustrate how to use the PySFTP library with comprehensive examples.
Installation
Before we begin, we need to install the
pysftp
library. You can do it using pip, the Python package manager. Please execute the following command in your command prompt or terminal:
Establishing an SFTP Connection
The first step in using PySFTP is to establish a connection with the server. Below is a simple example of how to accomplish this:
import pysftp
hostname = "example.com"
username = "user"
password = "pass"
# Establishing a connection
with pysftp.Connection(hostname, username=username, password=password) as sftp:
print("Connection successfully established with the server.")
In the example above, we are establishing a connection with the server 'example.com' using the given username and password. The
with
keyword is used to ensure the session is cleanly closed after we're done with it.
File Transfer
Let's move ahead and see how we can upload and download files from the server.
Upload a File:
with pysftp.Connection(hostname, username=username, password=password) as sftp:
local_path = "path/to/local/file"
remote_path = "path/on/server"
sftp.put(local_path, remote_path)
print("File successfully uploaded to the server.")
In the code snippet above, we used the
put()
function of the
pysftp.Connection
object to upload a file located at 'local_path' to the 'remote_path' on the server.
Download a File:
with pysftp.Connection(hostname, username=username, password=password) as sftp:
local_path = "path/to/local/file"
remote_path = "path/on/server"
sftp.get(remote_path, local_path)
print("File successfully downloaded from the server.")
In this snippet, we are using the
get()
function of the
pysftp.Connection
object to download a file located at 'remote_path' from the server to the 'local_path' on our local machine.
Working with Directories
You can also create, remove, or navigate through directories on the server.
Create a directory:
with pysftp.Connection(hostname, username=username, password=password) as sftp:
remote_directory_path = "/path/to/directory"
sftp.mkdir(remote_directory_path)
print(f"Directory {remote_directory_path} created on the server.")
Remove a directory:
with pysftp.Connection(hostname, username=username, password=password) as sftp:
remote_directory_path = "/path/to/directory"
sftp.rmdir(remote_directory_path)
print(f"Directory {remote_directory_path} removed from the server.")
Navigate through directories:
with pysftp.Connection(hostname, username=username, password=password) as sftp:
remote_directory_path = "/path/to/directory"
sftp.chdir(remote_directory_path)
print(f"Changed the current working directory to {remote_directory_path} on the server.")
In the examples above,
mkdir()
,
rmdir()
, and
chdir()
functions of the
pysftp.Connection
object are used to create, remove, and change the current working directory, respectively.
Conclusion
PySFTP is a straightforward and effective way to interact with SFTP servers using Python. While the examples given above cover some of the basics, the library provides numerous other features for managing and manipulating remote files and directories. For a full list of PySFTP's capabilities, it's recommended to refer to the official documentation.