我想使用pysftp 0.2.8从Windows目录上载多个文件到SFTP服务器。我已经阅读了该文档,并建议使用put_d
或,put_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()
我希望能够将所有文件或所选文件从本地目录复制到SFTP服务器。
我无法重现您的确切问题,但实际上pysftp的递归函数以一种使它们在Windows(或任何不使用* nix-like路径语法的系统)上失败的方式实现。
它对远程SFTP路径使用os.sep
并os.path
起作用,这是错误的,因为SFTP路径始终使用正斜杠。
但是您可以轻松实现便携式替换:
import os
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)
像这样使用它:
put_r_portable(sftp, sftp_local_path, sftp_remote_path, preserve_mtime=False)
有关的类似问题get_r
,请参见:
Linux上的Python pysftp get_r在Linux上可以正常运行,但在Windows上不能正常运行
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句