pysftp 连接更改目录不起作用
pysftp connection change directory doesn't work
我尝试了以下三种更改目录的方法,但它永远不会改变。还有其他人遇到过这个问题吗?
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
所以:
connection.chdir('/home/jm/project')
我尝试了以下三种更改目录的方法,但它永远不会改变。还有其他人遇到过这个问题吗?
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
所以:
connection.chdir('/home/jm/project')