Q:

Retrive latest directory

import paramiko
ssh =paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='test.com',username='test',password='test123')
srcpath = ('/tmp/test/')
destpath = ('/tmp/file/')
transfer=ssh.open_sftp()
stdin, stdout, stderr = ssh.exec_command('cd /tmp/test/; ls -1t *txt* | head -1') 
out = stdout.read().splitlines()
print out
error = stderr.read().splitlines()
print error
transfer.close()
ssh.close()

Above is my code, i tried to retrieve the latest directory on remote server. I am facing below error.
Error:

['bash: head: command not found']

is there any other way to retrieve latest directory ?

A:

You actually don't need "head" nor "tail", just access the last line from python as follows.
This is your code little edited to catch the last line as last_line:
import paramiko
ssh =paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='test.com',username='test',password='test123')
srcpath = ('/tmp/test/')
destpath = ('/tmp/file/')
transfer=ssh.open_sftp()
stdin, stdout, stderr = ssh.exec_command('cd /tmp/test/; ls -1t *txt*') 
out = stdout.read().splitlines()
last_line = out[-1] ## takes out the last line without need for tail command
print out
print last_line
error = stderr.read().splitlines()
print error
transfer.close()
ssh.close()

