Python pysftp put_r在Windows上不起作用

MdM

我想使用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服务器。

马丁·普里克里(Martin Prikryl)

我无法重现您的确切问题,但实际上pysftp的递归函数以一种使它们在Windows(或任何不使用* nix-like路径语法的系统)上失败的方式实现。

它对远程SFTP路径使用os.sepos.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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

pysftp库在AWS Lambda层中不起作用

pysftp与Paramiko

来自Linux的Python pysftp get_r在Linux上工作正常,但在Windows上工作不正常

gsutil在Mac和python3.5上不起作用

python pip install在Windows上不起作用

通过pysftp通过HTTP代理进行Python连接

使用Python的pysftp,如何验证主机密钥?

Python getpass在Windows Git Bash(MINGW64)上不起作用

pysftp连接更改目录不起作用

Python脚本在Windows上不起作用(但在Mac上可用)

Python nltk在Pycharm上不起作用

如何使用特定的HostkeyAlgorithms配置pysftp / paramiko连接-python

pysftp使用get_r()复制远程目录

Python pysftp.put引发“没有这样的文件”异常,尽管文件已上传

使用Python的PySFTP和get_r-“无此文件或目录”

控制台上的Spyder停止按钮在Windows和Python 3.7上不起作用

Pyinstaller 3.4在使用Python 2.7的Windows 10上不起作用

Python pysftp下载失败,并显示“ IOError:[Errno 21]是目录”

如何在Python中使用pysftp“ put”方法将多个.csv文件传送到SFTP服务器

Subprocess.Popen中的Python双引号在Windows 10上不起作用

Python子进程在Windows 7上不起作用

Python IDLE在Mac上不起作用

ReportLab在Azure Python网站上不起作用

点和分发在Python 3.3上不起作用

Boost Python模块在Windows XP上不起作用

Python 脚本在 crontab 上不起作用

python - http.server 在 Windows 上工作,但在 debian 上不起作用

python exifread在linux和mac上对图像进行排序,在windows上不起作用

Python -V 在 Windows 10 上不起作用或返回任何错误