简体   繁体   English

Python脚本在几个小时后停止

[英]Python script stops after a few hours

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. 我有一个Python 3脚本运行什么从网上下载图像,但它几个小时后停止,我无法弄清楚原因。 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. 我在.csv文件中有URL,它使用csv文件的第1列中提供的名称保存图像。

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. 我已经尝试关闭“print(url)”,因为我认为这可能在某个时刻占用了太多内存,但这并没有成功。

This is my script: 这是我的脚本:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM