我有一个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脚本停止
发布评论