首頁  >  問答  >  主體

python - paramiko怎么判断远程目录是否存在?

如题:paramiko怎么判断远程目录是否存在?
在调用exec_command函数的时候,怎么判断远程目录是否存在呢? 因为这里不能传if进去。

伊谢尔伦伊谢尔伦2740 天前2695

全部回覆(1)我來回復

  • 怪我咯

    怪我咯2017-04-18 09:06:49

    可以用笨一點方法,執行一些目錄偵測的指令,如ls,然後去偵測輸出!

    stdin,stdout,stderr = client.exec_command('ls DIR')
    if stdout.readline() != '':
        print("exist")
    else:
        print("not exist")
        
    

    方法二:
    使用sftp, 不使用exec_command

    sftp = client.open_sftp()
    try:
        sftp.stat(path)
        print("exist")
    except IOError:
        print("not exist")
    

    回覆
    0
  • 取消回覆