Python程序停在中间

2024-04-26 22:42:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个python程序来使用图像url下载图像列表。图像URL存储在一个文件中。python程序从文本文件创建url列表并下载图像。我正在使用urllib模块下载图像文件并将其存储在本地。我正在控制台中打印下载的图像url。你知道吗

这里的问题是,当启动时,程序工作正常,并根据需要下载图像,但突然之间,程序暂停,并没有进一步下载图像。它没有停止或抛出任何异常,它只是暂停,我可以看到控制台上下载的最后一个图像。然后我要重新启动程序继续图像下载过程。你知道吗

有人能帮我解决这个问题吗? 下面是我编写的代码:

import urllib
import hashlib
import os

f = open("/home/padmin/Image_Download/image_urls.txt")
to_download = f.readlines()
f.close()

downloaded = set()
try:
    f = open("Log.txt")
    downloaded = f.readlines()
    f.close()
except:
    print 'log opening error'
    pass

not_aval = open("/home/padmin/Image_Download/Image_Not_Available.txt",'a')
log_file= open("/home/padmin/Image_Download/Log.txt",'a')

to_download = set(to_download) - set(downloaded)

path = '/home/padmin/Image_Download/Images/'

if not os.path.exists(path):
    os.makedirs(path)

for line in to_download:
    line = line.strip()
    print line
    h=hashlib.sha1(line).hexdigest()
    try:
        f = open(path+h+'.jpg','wb')
        f.write(urllib.urlopen(line).read())
        f.close()
        log_file.write(line+'\n')

    except Exception,e:
        print e
        try:
            not_aval.write(line+'\n')

        except Exception, e:
            print "exception: ",e

not_aval.close()
log_file.close()`

Tags: topath图像image程序txtloghome