几个小时后Python脚本停止

编程入门 行业动态 更新时间:2024-10-27 18:31:40
本文介绍了几个小时后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脚本停止

本文发布于:2023-11-27 02:02:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   脚本   小时后   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!