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)

 

转载于:https://www.cnblogs.com/deartear/p/8616457.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值