我尝试了以下三种更改目录的方法,但它从未更改。还有其他人有这个问题吗?
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/'
确实存在。我还尝试了许多其他未在此处列出的组合。
对我来说没有任何意义,你能帮忙吗?
答案 0 :(得分:1)
改为尝试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')