Making a connection to an SFTP server and searching for a particular file and directory inside the server is essential when it comes to the upload and the download of data from the SFTP server. In the last tutorial we learned how to make a successful connection with the SFTP server. In this one, we are going to learn how to get a directory path in the SFTP server.
Connect To SFTP Server
First, let’s establish a connection with the SFTP server.
import pysftp host = 'test.rebex.net' port = 22 username = 'demo' password= 'password' try: conn = pysftp.Connection(host=host,port=port,username=username, password=password) print("connection established successfully") except: print('failed to establish connection to targeted server')
Current Working Directory Using Pwd
So we have “conn” as a connection object to our targeted SFTP server, “test.rebex.net”. Whenever we establish such a connection, we need to find the path of the current working directory. Just like in Linux, our conn object provides the print working directory (pwd) attribute to print the current working directory. So, we call conn.pwd which will print a string path of current working directory as:
List Of Files Using listdir()
As you can see, we are right in “/” (root directory of our server). Now, we want to list all the directories and the files in the current working directory. We can use conn.listdir() method to print list of files and directories available in the current directory.
As you can see, conn.listdir() has returned a list containing three names. “aspnet_client” and “pub” are directories while readme.txt is a file.
Changing Path Using cd()
There’s more in this tutorial on how to get a directory path in the SFTP server. While working with the server, we have to move to multiple directories. To move to a particular directory, we can pass the path of the directory to conn.cd() along with the “with” statement. Normally, in the Linux operating system’s command line we call cd to change directory, but in the case of pysftp, we have to use with statement and conn.cd() to change path. Let’s say we want to move to ‘/pub/example/’ directory and perform some operations.
If we simply call con.cd(‘/pub/example’) and then print pwd, it will not change directory.
As you can see, we are still in root directory. So let’s try with a statement along with conn.cd() in the following manner:
with conn.cd('/pub/example'): print(conn.pwd)
It changed the path successfully. Remember, this change of path takes effect within that block. Once the “with” statement block is closed, you will be directed back to the root directory.
Let’s go to particular directory and print all directories and files.
with conn.cd('/pub/example'): print(conn.listdir())
Validating Path Using exists()
If we are provided some path, and we want to validate it, we can use the conn.exists() method. To validate any path, simply pass that path as argument to conn.exists(). If the path is correct, it will return “True” or else it will come back as “False”.
In this tutorial, we learned how to get the path of a current working directory, how to change the path to a particular directory and how to validate a given path. pwd returns string path of current working directory. To change the directory, we use the conn.cd() method. You must remember conn.cd() does not work until we use it with the “with” statement. conn.cd() takes effect only inside with the block statement. Also, the con.exists() method is used to validate any path.