Sometimes, we want to do SFTP in Python
In this article, we’ll look at how to do SFTP in Python.
How to do SFTP in Python?
To do SFTP in Python, we can use the fsspec
library.
To install it, we run
pip install fsspec
Then we use it by writing
from fsspec.implementations.sftp import SFTPFileSystem
fs = SFTPFileSystem(host=host, username=username, password=password)
fs.ls("/")
with fs.open(file_name) as file:
content = file.read()
to create a SFTPFileSystem
object to connect to the host
with the username
and password
.
Then we call ls
to list the root directory of the server.
And then we call fs.open
to open the file with the file_name
.
Conclusion
To do SFTP in Python, we can use the fsspec
library.