几个小时后Python脚本停止 [英] Python script stops after a few hours

查看:67
本文介绍了几个小时后Python脚本停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python 3脚本运行从网络上下载图像的脚本,但是几个小时后它停止了,我不知道为什么.我在.csv文件中有URL,它以csv文件第1列中提供的名称保存图像.

I have a Python 3 script running what download images from the web, but it stops after a few hours and I can't figure out why. I have URL's in a .csv file and it saves the images with the name what is provided in column 1 of the csv file.

我已经试图关闭打印(URL)",因为我认为这可能在某个时刻占用了过多的内存,但这并没有解决问题.

I already tried to switch off the "print (url)", because I thought that this maybe took too much memory at a certain moment, but that didn't do the trick.

这是我的脚本:

import csv
import requests
print ('download.py map 3_new')

with open('3.csv') as csvfile:
    csvrows = csv.reader(csvfile, delimiter=';', quotechar='"')
    for row in csvrows:
        filename = row[0]
        url = row[1]
        #print (url)
        result = requests.get(url, stream = True)
        if result.status_code == 200:
            image = result.raw.read()
            open(filename,"wb").write(image)

推荐答案

很有可能是由于将图像保存到硬盘后没有关闭文件而引起的.尝试通过这种方式做到这一点:

There is a good chance that it's caused by not closing the files after saving images to your hard drive. Try to do it this way:

import csv
import requests
print ('download.py map 3_new')

with open('3.csv') as csvfile:
    csvrows = csv.reader(csvfile, delimiter=';', quotechar='"')
    for row in csvrows:
        filename = row[0]
        url = row[1]
        #print (url)
        result = requests.get(url, stream = True)
        if result.status_code == 200:
            image = result.raw.read()
            with open(filename,"wb") as f:
                f.write(image)

这篇关于几个小时后Python脚本停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆