Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pysftp连接更改目录不存在';行不通_Python_Ssh_Cd_Pysftp - Fatal编程技术网

Python pysftp连接更改目录不存在';行不通

Python pysftp连接更改目录不存在';行不通,python,ssh,cd,pysftp,Python,Ssh,Cd,Pysftp,我尝试了以下三种方法来更改目录,但它从不更改。还有其他人有这个问题吗 import pysftp cnopts = pysftp.CnOpts() cnopts.hostkeys = None host = 'server1.mydomain.com' # altered with pysftp.Connection(host=host, **get_credentials(), cnopts=cnopts) as connection: connection.execute('cd

我尝试了以下三种方法来更改目录,但它从不更改。还有其他人有这个问题吗

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
host = 'server1.mydomain.com' # altered
with pysftp.Connection(host=host, **get_credentials(), cnopts=cnopts) as connection:

    connection.execute('cd project')
    print(connection.execute('pwd'))      #---> [b'/home/jm\n']

    connection.execute('cd /home/jm/project')
    print(connection.execute('pwd'))      #---> [b'/home/jm\n']

    connection.cd('project')
    print(connection.execute('pwd'))      #---> [b'/home/jm\n']

    with connection.cd('project'):
        print(connection.execute('pwd'))  #---> [b'/home/jm\n']
顺便说一下,
“/home/jm/project/”
确实存在。我还尝试了许多我没有在这里列出的其他组合

这对我来说没有任何意义,你能帮忙吗?

试试
chdir()
。根据文件:

cd(remotepath=None)
context manager that can change to a optionally specified remote directory and restores the old pwd on exit.

chdir(remotepath)
change the current working directory on the remote
因此:


但最终,这也没有奏效。也许这是PYSTFP与centos 7或其他系统对话的问题。工作原理是:我必须移动到目录并立即执行所需的命令。否则,它会在该命令执行完毕后直接将我移回
connection.execute('cd/home/jm/project;ls')
换句话说,它好像没有当前工作目录更改的持久性。在本例中,cd是上下文感知的,如文档所示:。因此,当函数结束时,它将恢复到原始路径。
connection.chdir('/home/jm/project')