我一直收到这个错误告诉我缩进我的块但是我没有看到我需要这样做,特别是如果我正在运行两个try子句。我试图允许我的第二个try子句打印到第一个日志。以下是我到目前为止的情况:
#!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')
#get objects
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')
for line in f:
try:
for row in f:
if solution_id != row[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 row
finally:
print "all error checks done!"
答案 0 :(得分:0)
什么是
else:
print row
连接到?它应该从需要else的东西缩进,但它在第1列......
答案 1 :(得分:0)
在第二次尝试声明中。你有和if语句没有行动
for row in f:
try:
if solution_id != row[2]:
except ValueError:
logging.error('Solution ID is invalid. Please check the number and try again')
需要有类似
的东西 for row in f:
try:
if solution_id != row[2]:
print "row error"
except ValueError:
logging.error('Solution ID is invalid. Please check the number and try again')
if语句试图在条件之后使用except作为动作。
EDIT :: 我没有cvs来测试。但我没有从这段代码中得到任何其他错误:
#!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')
#get objects
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')
for line in f:
try:
for row in f:
if solution_id != row[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 row
finally:
print "all error checks done!"