MdM*_*MdM 1 python upload sftp pysftp
我想使用 pysftp 0.2.8 将多个文件从 Windows 目录上传到 SFTP 服务器。我已经阅读了文档,它建议使用put_dorput_r但两者都给我以下错误:
OSError:无效路径:
sftp_local_path = r'C:\Users\Swiss\some\path'
sftp_remote_path = '/FTP/LPS Data/ATC/RAND/20191019_RAND/XML'
with pysftp.Connection("xxx.xxx.xxx.xxx", username=myUsername, password=myPassword) as sftp:
with sftp.cd(sftp_remote_path):
sftp.put_r(sftp_local_path, sftp_remote_path)
for i in sftp.listdir():
lstatout=str(sftp.lstat(i)).split()[0]
if 'd' in lstatout: print (i, 'is a directory')
sftp.close()
Run Code Online (Sandbox Code Playgroud)
我希望能够将本地目录中的所有文件或选定文件复制到 SFTP 服务器。
我无法重现您的确切问题,但确实已知 pysftp 的递归函数的实现方式使它们在 Windows(或任何不使用类似 *nix 的路径语法的系统)上失败。
Pysftp 使用os.sep和os.path作用于远程 SFTP 路径,有什么问题,因为 SFTP 路径总是使用正斜杠。
但是您可以轻松实现便携式替换:
import os
Run Code Online (Sandbox Code Playgroud)
def put_r_portable(sftp, localdir, remotedir, preserve_mtime=False):
for entry in os.listdir(localdir):
remotepath = remotedir + "/" + entry
localpath = os.path.join(localdir, entry)
if not os.path.isfile(localpath):
try:
sftp.mkdir(remotepath)
except OSError:
pass
put_r_portable(sftp, localpath, remotepath, preserve_mtime)
else:
sftp.put(localpath, remotepath, preserve_mtime=preserve_mtime)
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
put_r_portable(sftp, sftp_local_path, sftp_remote_path, preserve_mtime=False)
Run Code Online (Sandbox Code Playgroud)
有关关于 的类似问题get_r,请参阅:
Python pysftp get_r from Linux 在 Linux 上工作正常,但在 Windows 上不工作
| 归档时间: |
|
| 查看次数: |
1433 次 |
| 最近记录: |