gpt4 book ai didi

python - pysftp 连接更改目录不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 02:19:50 24 4
gpt4 key购买 nike

我尝试了以下三种更改目录的方法,但它永远不会改变。还有其他人遇到过这个问题吗?

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

关于python - pysftp 连接更改目录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com