1

Actually, I need to check for a file using Paramiko. I am getting the following error. It would be helpful if any of you could resolve this.

import paramiko
try:
    host = '23.120.7.00'
    username = 'UNAME1'
    password = 'UNAME12'
    file_to_check = '\\\\ipconnect\\ABCS\\IPXADEP\\file1.dta'
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host,username=username, password=password)
    channel = client.get_transport().open_session()
    channel.exec_command('cd \\\\ipconnect\\ABCS\\IPXADEP')
    stdin, stdout, stderr = channel.exec_command('ls')
    stdout = channel.makefile().read()
    output = stdout.decode('utf-8').split('\n')[:-1]
    print(output)
    client.close()
except Exception as e:
    print(str(e))

I am getting the following error:

cannot unpack non-iterable NoneType object

I need to check whether the file mentioned above is present or not in that particular folder. Why isn't it possible to list out the files? Any help would be highly appreciated.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
anaghapramesh
  • 77
  • 1
  • 2
  • 11

1 Answers1

1

First, your code cannot work this way.

See Execute multiple commands in Paramiko so that commands are affected by their predecessors


But to manipulate files, you should not use shell commands anyway. Use the standard SSH API for manipulating files, the SFTP:

sftp = client.open_sftp()

stat = sftp.stat(path_to_file)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992