python如何跳过错误继续运行,同时删除产生错误的文档

因为我用的package有bug有些文档不能处理当程序在读取这个文件的时候会出现math domain error,所以我现在要实现的目的就是跳过这些error,同时删除产生error的文档。

我的code如下所示:
首先建立一个excel文档,因为我需要把算的结果导出到excel里面,
然后就用了一个for loop直接运行上面所写的三个method,请前辈帮忙在我现有的code基础改一下达到我想要实现的目的。

import os,csv,nltk, math
from nltk.model.ngram import NgramModel
from nltk.probability import LidstoneProbDist

#open the csv file
fout = open("/Users//WN1.data.csv", "w")

outfilehandle = csv.writer(fout,
delimiter=",",
quotechar='"',
quoting=csv.QUOTE_NONNUMERIC)

localrow = []
localrow.append("File name")
localrow.append("Perplexity for unigram")
localrow.append("Perplexity for bigram")
localrow.append("Perplexity for trigram")
outfilehandle.writerow(localrow)

# unigram model
def unigram(file):
#read file
file_object = open(file)
ln=file_object.read()

words = nltk.word_tokenize(ln)
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
tt=NgramModel(1, words, estimator = estimator)

return tt.perplexity(words)

#bigram model
def bigram(file):
file_object = open(file)
ln=file_object.read()

words = nltk.word_tokenize(ln)
my_bigrams = nltk.bigrams(words)

#fdist = nltk.FreqDist(my_bigrams)
#lapalce smoothing
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
tt2=NgramModel(2, my_bigrams, estimator = estimator)

return tt2.perplexity(my_bigrams)

#trigram model
def trigram(file):
file_object = open(file)
ln=file_object.read()

words = nltk.word_tokenize(ln)
my_trigrams = nltk.trigrams(words)

#lapalce smoothing
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
tt3=NgramModel(3, my_trigrams, estimator = estimator)

return tt3.perplexity(my_trigrams)

#set the path of the folder
os.chdir("/Users/Documents/A")
s = os.getcwd()
#read files in the folder
files = os.listdir(s)
bg=0
for file in files:
uni = unigram(file)
bi=bigram(file)
tri=trigram(file)
localrow= []
localrow.append(file)
localrow.append(uni)
localrow.append(bi)
localrow.append(tri)
outfilehandle.writerow(localrow)

fout.close()

这就需要用到python的异常处理机制:

try:
    raise Exception('Oh no!产生了一个异常!')
except Exception, e:
    print '发生了一个错误: %s, 你可以在这里删除错误的文档' % e
finally:
    print '这里无论是否发生异常, 都会执行'
else:
    print '在没有发生异常时执行'

# 继续下面的流程

 

参考以上代码修改你的程序。望采纳。

追问

我自己试了一下在except后面加上os.remove(file)
报错WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'a.txt'
我猜测的原因是我后面的loop还在使用这个会出错的文档,请问这样如何处理。
谢谢

追答

把文件关闭即可。调用文件的close()方法。

追问

我在def里面用了close()了,但是还不对,close应该用在那里怎么用? 

追答

file_object.close()

温馨提示:答案为网友推荐,仅供参考
相似回答