【问题标题】:Unable to change directory using Python Paramiko无法使用 Python Paramiko 更改目录
【发布时间】:2020-04-21 13:53:53
【问题描述】:

实际上,我需要使用 Paramiko 检查文件。我收到以下错误。如果你们中的任何人能解决这个问题,将会很有帮助。

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))

我收到以下错误:

无法解压不可迭代的 NoneType 对象

我需要检查上述文件是否存在于该特定文件夹中。为什么不能列出文件?任何帮助将不胜感激。

【问题讨论】:

    标签: python ssh sftp paramiko pysftp


    【解决方案1】:

    首先,您的代码不能这样工作。

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


    但是要操作文件,无论如何你都不应该使用 shell 命令。使用标准 SSH API 来操作文件,SFTP

    sftp = client.open_sftp()
    
    stat = sftp.stat(path_to_file)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2019-11-02
      • 1970-01-01
      • 2020-11-08
      • 2011-05-23
      相关资源
      最近更新 更多