python - 无法使用 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)
解决方案
通常 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 中的单行。
推荐阅读
- react-testing-library - 在测试组件方面,Preact 测试库是否与 React 测试库相同?
- excel - 可以在 vba 中处理参数变量吗?
- visual-studio-code - 有没有办法在 VSCode 中抑制/延迟智能感知和用户帮助?
- html - 如何使用 flex-direction: column; 创建一个表?
- sql - PostgreSQL:如何为包含值标识的每一行提取相关数据
- python - 读取多个文件并按列保存到 xls(pypdf2 和 xlsxwriterr
- android - 将屏幕显示从 Android Emulator 重定向到服务器
- python - 在 Python3 二进制字符串中允许空字节
- azure - 将 Azure AD MFA 与 Citrix ADC 结合使用
- javascript - 输入与 Array 对象的匹配值