使用 python 从 SFTP 拉取文件

发布于 2025-01-14 03:51:07 字数 660 浏览 3 评论 0原文

from msilib.schema import Directory
import pysftp
import os
import glob
import fnmatch
from datetime import date, timedelta
 
cnopts = pysftp.CnOpts()

cnopts.hostkeys = None

myHostname = 'sftp.mmm.com'
myUsername = 'uuuu'
myPassword = 'pass'

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
    print("Connection Success")

remotefilepath='/REPORTING/test.zip'

localfilepath='Z:\\data\\sftp_data\\'

sftp.get(remotefilepath,localfilepath)

大家好 我一直在使用上面的代码从SFTP提取文件并保存在本地 但是我遇到了以下错误

chan = t.open_session( AttributeError:“NoneType”对象没有属性“open_session”

请告知

from msilib.schema import Directory
import pysftp
import os
import glob
import fnmatch
from datetime import date, timedelta
 
cnopts = pysftp.CnOpts()

cnopts.hostkeys = None

myHostname = 'sftp.mmm.com'
myUsername = 'uuuu'
myPassword = 'pass'

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
    print("Connection Success")

remotefilepath='/REPORTING/test.zip'

localfilepath='Z:\\data\\sftp_data\\'

sftp.get(remotefilepath,localfilepath)

Hi All
I have been using above code to pull the file from SFTP and save locally
However i am getting below error

chan = t.open_session(
AttributeError: 'NoneType' object has no attribute 'open_session'

Please advise

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听,心雨的声音 2025-01-21 03:51:07

with语句结束,打印结束后,连接自动关闭。这就是 with 的用途。更改为简单脚本

sftp = pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts)

或缩进脚本的其余部分,使其位于 with 内:

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
    print("Connection Success")
    remotefilepath='/REPORTING/test.zip'
    localfilepath='Z:\\data\\sftp_data\\'
    sftp.get(remotefilepath,localfilepath)

When the with statement ends, after the print, the connection is automatically closed. That's what with is for. Either change to a simple

sftp = pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts)

or indent the rest of the script so it's inside the with:

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
    print("Connection Success")
    remotefilepath='/REPORTING/test.zip'
    localfilepath='Z:\\data\\sftp_data\\'
    sftp.get(remotefilepath,localfilepath)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文