Python 设置检查点简单实现,python检查,[Python]代码#


[Python]代码

# 这个异常是原文本内容中未出现检查点内容出现造成的   class CheckPointMissContentError:       pass  # 将文件读取指针fd移至到检查点对应的内容处   # check point 的规则为,读取文件一行或者多行,进行操作后,将此一行或多行送入   # 检查文件check_point中。以后再次运行程序,即可从该检查点处继续运行。   def GoCheckPoint(fd,check_point):       if not os.path.isfile(check_point):           f_check = open(check_point,'w')           f_check.close()       f_check = open(check_point,'r')       lines = f_check.readlines()       if len(lines) > 0:           check_content = lines[-1] #找到检查点最后一行           check_content = check_content.strip(' \n\r')           # go to check point           while True:               content = fd.readline()               if content == '': # eof                   raise CheckPointMissContentError               if content.strip(' \n\r') == check_content:                   break      f_check.close()#关闭检查点  

[Python]代码

# 伪代码   def Download(downloadlist,sleep_time):       if os.path.isfile(downloadlist):           f = open(downloadlist)          # check_point file name,这里为自动生成一个检查点文件           check_point = file[0:file.rfind('.')]+'_check.txt'          Util.GoCheckPoint(f,check_point) #这就是上面代码中的GoCheckPoint函数           f_check = open(check_point,'a')# 以追加方式写入           try:               while True:                   content = f.readline()                   if content == '': # eof                       break                  content = content.strip(' \n\r')                   if content != '':                       # has download url                       time.sleep(sleep_time)                       DownloadOper(path,url) #这里是伪代码..可以认为是urllib.request.retrieve()函数或者是urllib.request.urlopen()啥的                  # 作为响应的操作后再将内容写入检查点文件                       f_check.write(content+'\n')                       f_check.flush() # 必须的,否则会缓存,不会写入硬盘中           except : # 蹦个异常也不怕,以后再次按F5执行即可               raise Exception()               return Util.FAILURE # 这是我设置的常量,大家认为是0或者1就可以了           finally:               f.close()               f_check.close()# 关闭文件           print('Downloading  is done........................')           return Util.SUCCESS  

评论关闭