我正在运行一个forloop并且运行正常,但它仍然会在文件的最后一行显示它,即使它有错误:打印输出看起来像这样:最后一行应该看起来像其余部分。
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
Solution ID is invalid. Pleae check the number and try again
['Delete', 'Service Group', 'CR-Web-ISD']
all error checks done!
这是我到目前为止所做的:
#!usr/bin/python
from subprocess import *
import sys
import ConfigParser
import os
import csv
import getopt
import time
import datetime
from datetime import date
from time import gmtime, strftime
import logging
from sys import argv
script, solution_id, input_file = argv
#creating time stamp and returning as a string to add to solution id log name
def timeIzNow():
full = time.strftime(" %Y-%m-%d %H:%M:%S")
return full
#set up logging to file[
LOG_FILENAME = solution_id + timeIzNow()
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s %(process)d',
datefmt='%d %b %Y %H:%M:%S',
filename=LOG_FILENAME,
filemode='w')
# defining a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# setting a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# telling the handler to use this format
console.setFormatter(formatter)
# adding the handler to the root logger
logging.getLogger('').addHandler(console)
#set up configuration Parser
config = ConfigParser.RawConfigParser()
config.read('/etc/nagios/ingestion/objectItems.cfg')
config.read('/etc/nagios/ingestion/action.cfg')
#set up configuration Parser to get objects and check the csv file for matches
objects = config.get('Objects', 'objects')
#get actions
actions = config.get('Actions', 'actions')
#if no object is found, run error
assert(sys.argv[1] != None), "object does not exist"
#logging debug
#logging.debug('object does not exist')
#Get inputs and check value and path to file
try:
f = csv.reader(open(input_file, "rb"))
except:
logging.error('No such file or directory. Please try again')
else:
try:
for line in f:
for column in f:
if solution_id != column[2]:
print "Solution ID is invalid. Pleae check the number and try again"
except ValueError:
logging.error('Solution ID is invalid. Please check the number and try again')
else:
print column
finally:
print "all error checks done!"
答案 0 :(得分:1)
查看你的代码(你真的需要引用这部分):
try:
for line in f:
for column in f:
if solution_id != column[2]:
print "Solution ID is invalid. Pleae check the number and try again"
except ValueError:
logging.error('Solution ID is invalid. Please check the number and try again')
else:
print column
这说
try
)尝试运行以下循环。except ValueError
)如果在任何时候遇到ValueError,请记录错误并在循环后继续。 (如果遇到任何其他类型的异常,它将被抛出,程序将执行finally
块并终止。)else
)如果您完成整个循环并且没有遇到异常,则打印变量column
的当前内容。由于循环完成而没有引发异常,因此执行else
子句。由于循环已经运行完成,因此column
的当前值是从文件的最后一行读取的。
您没有向我们展示输入文件,但我还要下注,除了常规输出之外,您还要执行print column
语句 最后一行,而不是而不是那个输出。