zoukankan      html  css  js  c++  java
  • python 读取csv中的文件,从sftp下载文件

    需要从sftp上下载一些图片文件,文件名存放在一个csv文件中。代码如下:

    # -*- coding:utf-8 -*-
    import paramiko
    import csv
    import os
    
    def sft_download_all(host,port,username,password):
        sf = paramiko.Transport((host,port))
        sf.connect(username = username,password = password)
        sftp = paramiko.SFTPClient.from_transport(sf)
    
        csv_reader1=csv.reader(open(r'D:数据处理photo.csv',encoding='utf-8'))
        for row in csv_reader1:
            remote=row[2]
            filename=row[1]
            localfile='D:\训练数据\all\'+filename
            try:
                if sftp.stat(remote) and (not os.path.exists(localfile)):
                    print("download:"+remote)
                    sftp.get(remote,localfile)
            except Exception as e:
                print('出错跳过:file='+remote+' exception:',e)
                continue
        print("全部数据下载完毕!")
    
    def sftp_download(host,port,username,password):
        sf = paramiko.Transport((host,port))
        sf.connect(username = username,password = password)
        sftp = paramiko.SFTPClient.from_transport(sf)
    
        csv_reader1=csv.reader(open(r'D:数据处理系安全带的.csv',encoding='utf-8'))
        for row in csv_reader1:
            remote=row[4]
            filename= os.path.basename(remote)
            localfile='D:\训练数据\hasbelt\'+filename
            try:
                if sftp.stat(remote) and (not os.path.exists(localfile)):
                    print("download:"+remote)
                    sftp.get(remote,localfile)
            except Exception as e:
                print('出错跳过:file='+remote+' exception:',e)
                continue
        print("通过数据下载完毕!")
    
        csv_reader2=csv.reader(open(r'D:数据处理没系安全带.csv',encoding='utf-8'))
        for row in csv_reader2:
            remote=row[4]
            filename= os.path.basename(remote)
            localfile='D:\训练数据\beltno\'+filename
            try:
                if sftp.stat(remote) and (not os.path.exists(localfile)):
                    print("download:"+remote)
                    sftp.get(remote,localfile)
            except Exception as e:
                print('出错跳过:file='+remote+' exception:',e)
                continue
        print("不通过数据下载完毕!")
        sf.close()
    
    if __name__ == '__main__':
        host = '172.16.207.21'#主机
        port = 22 #端口
        username = 'root' #用户名
        password = 'data' #密码
        sftp_download(host,port,username,password)#下载
        #sft_download_all(host,port,username,password)
  • 相关阅读:
    网络知识梳理--OSI七层网络与TCP/IP五层网络架构及二层/三层网络(转)
    OSI七层模型与TCP/IP五层模型(转)
    C语言结构体指针初始化(转)
    关于空指针NULL、野指针、通用指针 (转)
    C语言的通用指针类型(void *)
    使用matlab生成用于ROM初始化的coe文件(转)
    电脑运行msi安装包提示the error code is 2503/2502如何解决
    mysql命令行修改字符编码
    struts 2 时间控件
    java 的 struts2 Spring Hibernate 三大框架的整合
  • 原文地址:https://www.cnblogs.com/deartear/p/8616457.html
Copyright © 2011-2022 走看看