在我的工作代码中,我有这个
import paramiko
parent=os.path.split(dir_local)[1]
for walker in os.walk(parent):
try:
self.sftp.mkdir(os.path.join(dir_remote,walker))
except:
pass
for file in walker[2]:
sftp.put(os.path.join(walker[0],file),os.path.join(dir_remote,walker[0],file))
现在显示的错误是
Trying ssh-agent key 5e08bb83615bcc303ca84abe561ef0a6 ... success
Caught exception: <type 'exceptions.IOError'>: [Errno 2] Directory does not exist.
打印 walker
显示该文件夹内的所有文件,但我不知道为什么该文件夹不复制到 sftp 服务器
最佳答案
除非您重写了 os.walk()
,否则它会生成一个包含三个对象的元组:dirpath、dirnames、filenames
所以,你在 os.path.join(dir_remote, walker)
调用总是会抛出异常,导致你期望的目录没有被创建。
我发现像这样编写 os.walk()
循环更清晰:
for dirpath, dirnames, filenames in os.walk(parent):
remote_path = os.path.join(dir_remote, dirpath)
# make remote directory ...
for filename in filenames:
local_path = os.path.join(dirpath, filename)
remote_fliepath = os.paht.join(remote_path, filename)
# put file
请记住,os.walk()
将遍历您给定的父级
中的任何目录。
关于python - paramiko 通过 sftp 上传文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24400733/