首页 > 解决方案 > 无法使用 pysftp 将文件夹上传到 Sftp

问题描述

不知道为什么这不起作用,我试图将整个目录从本地驱动器上传到 sftp,但不是上传文件夹,而是创建一个看起来像这样的文件“.\20200131\doc1.txt”?我已经在这几个小时了,我想我错过了一些东西

def upload_files_to_sftp():
    localpath = r"c:\test\" 
    remotepath = "/uploads/test_Unzipped"
    for root,dirs,_ in os.walk(r"c:\test"):
        for d in dirs:
            folders_to_upload = (os.path.join(root,d))
            print(folders_to_upload)
            with pysftp.Connection('mysftp', username='username', password='mypassword') as sftp:
                sftp.put_r(localpath , remotepath)

标签: pythonpysftp

解决方案


通常 sftp 服务器也会授予 ssh 访问权限。

如果您有 ssh 访问权限,您可以:

(cd /source/dir && tar --create --sparse --one-file-system .) | ssh example.com 'cd /target/dir && tar xfp -'

...例如,使用 cygwin tar 和 ssh。IOW,这可以很容易地简化为 shell 中的单行。


推荐阅读