check if directory exists on remote machine before sftp
Question:
this my function that copies file from local machine to remote machine with paramiko, but it doesn’t check if the destination directory exists and continues copying and doesn’t throws error if remote path doesn’t exists
def copyToServer(hostname, username, password, destPath, localPath):
transport = paramiko.Transport((hostname, 22))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(localPath, destPath)
sftp.close()
transport.close()
i want to check if path on remote machine exists and throw error if not.
thanks in advance
Answers:
I would use the listdir method within SFTPClient. You will probably have to use this recursively to ensure the entire path is valid.
You can use the chdir()
method of SFTPClient
. It checks if the remote path exists, raises error if not.
try:
sftp.chdir(destPath)
except IOError as e:
raise e
This will do
def copyToServer(hostname, username, password, destPath, localPath):
transport = paramiko.Transport((hostname, 22))
sftp = paramiko.SFTPClient.from_transport(transport)
try:
sftp.put(localPath, destPath)
sftp.close()
transport.close()
print(" %s SUCCESS " % hostname )
return True
except Exception as e:
try:
filestat=sftp.stat(destPath)
destPathExists = True
except Exception as e:
destPathExists = False
if destPathExists == False:
print(" %s FAILED - copying failed because directory on remote machine doesn't exist" % hostname)
log.write("%s FAILED - copying failed directory at remote machine doesn't existrn" % hostname)
else:
print(" %s FAILED - copying failed" % hostname)
log.write("%s FAILED - copying failedrn" % hostname)
return False
In my opinion it’s better to avoid exceptions, so unless you have lots of folders, this is a good option for you:
if folder_name not in self.sftp.listdir(path):
sftp.mkdir(os.path.join(path, folder_name))
To check if a remote server folder path exists using the sshpass and ssh commands in Python, you can use the command to include a check for the folder path using the test command.
Here’s an example python code snippet:
import os
# Define the command to execute
remote_path = '/path/to/folder'
cmd = f'sshpass -p pass ssh username@ip test -d "{remote_path}" && echo "Exists" || echo "Does not exist"'
# Execute the command using os.system
output = os.popen(cmd).read().strip()
# Check the output and print a message
if output == 'Exists':
print(f'The path {remote_path} exists on the remote server')
else:
print(f'The path {remote_path} does not exist on the remote server')
This code constructs a sshpass and ssh command that checks if the specified folder path exists on the remote server using the test command. If the folder path exists, the command will output Exists
, and if it doesn’t exist, it will output Does not exist
. The code captures the output of the command using the os.popen
function, checks the output for the existence of the folder path, and prints a message indicating whether it exists or not.
this my function that copies file from local machine to remote machine with paramiko, but it doesn’t check if the destination directory exists and continues copying and doesn’t throws error if remote path doesn’t exists
def copyToServer(hostname, username, password, destPath, localPath):
transport = paramiko.Transport((hostname, 22))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(localPath, destPath)
sftp.close()
transport.close()
i want to check if path on remote machine exists and throw error if not.
thanks in advance
I would use the listdir method within SFTPClient. You will probably have to use this recursively to ensure the entire path is valid.
You can use the chdir()
method of SFTPClient
. It checks if the remote path exists, raises error if not.
try:
sftp.chdir(destPath)
except IOError as e:
raise e
This will do
def copyToServer(hostname, username, password, destPath, localPath):
transport = paramiko.Transport((hostname, 22))
sftp = paramiko.SFTPClient.from_transport(transport)
try:
sftp.put(localPath, destPath)
sftp.close()
transport.close()
print(" %s SUCCESS " % hostname )
return True
except Exception as e:
try:
filestat=sftp.stat(destPath)
destPathExists = True
except Exception as e:
destPathExists = False
if destPathExists == False:
print(" %s FAILED - copying failed because directory on remote machine doesn't exist" % hostname)
log.write("%s FAILED - copying failed directory at remote machine doesn't existrn" % hostname)
else:
print(" %s FAILED - copying failed" % hostname)
log.write("%s FAILED - copying failedrn" % hostname)
return False
In my opinion it’s better to avoid exceptions, so unless you have lots of folders, this is a good option for you:
if folder_name not in self.sftp.listdir(path):
sftp.mkdir(os.path.join(path, folder_name))
To check if a remote server folder path exists using the sshpass and ssh commands in Python, you can use the command to include a check for the folder path using the test command.
Here’s an example python code snippet:
import os
# Define the command to execute
remote_path = '/path/to/folder'
cmd = f'sshpass -p pass ssh username@ip test -d "{remote_path}" && echo "Exists" || echo "Does not exist"'
# Execute the command using os.system
output = os.popen(cmd).read().strip()
# Check the output and print a message
if output == 'Exists':
print(f'The path {remote_path} exists on the remote server')
else:
print(f'The path {remote_path} does not exist on the remote server')
This code constructs a sshpass and ssh command that checks if the specified folder path exists on the remote server using the test command. If the folder path exists, the command will output Exists
, and if it doesn’t exist, it will output Does not exist
. The code captures the output of the command using the os.popen
function, checks the output for the existence of the folder path, and prints a message indicating whether it exists or not.