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)
复制代码

 

posted @   哈莫米拉  阅读(1290)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示