csv文件没有读取命令行输入参数

时间:2014-12-15 19:53:34

标签: python csv

好的,我查看了csv模块并更新了我的代码。我有csv到位并且文件正在被读取但是我的输入值没有给我一个错误,就像它应该放错了。这就是我现在拥有的:

#!usr/bin/python


from subprocess import *
import sys
import ConfigParser
import os
import csv
import getopt
import time
import datetime
import logging
from sys import argv
script, solution_id, input_file = argv

#set up logging to file
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%d %b %Y %H:%M:%S',
                    filename='/etc/nagios/ingestion/logs/catch.log',
                    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
def print_all(f):
    f.read()

# place an exception for incorrect path
try:
    current_file = csv.reader(open(input_file, "rb"))
    for row in current_file: solution_id = row[2]
    if solution_id != row[2]:
        print "invalid solution id"

#list exceptions  
except IOError:
    #logging error
    logging.error('No such file or directory. Please try again')


except IOError:
    print("Solution id is invalid. Please check the number and try again")
    #logging error
    logging.error('Solution id is invalid. Please check the number and try again')     

2 个答案:

答案 0 :(得分:1)

查看代码的最后部分,我会看到一些探测器 - 或潜在的探测器

# place an exception for incorrect path
try:
    current_file = csv.reader(open(input_file, "rb"))

您的意思是将文件打开为二进制文件吗?我想你"rt"

    for row in current_file: solution_id = row[2]

最后一行读取文件,从每行中提取row[2]。结果是solution_id将只具有文件中最后一行的值。如果文件为空(即循环从不执行)

,它也将在下一个语句中未定义
    if solution_id != row[2]:
        print "invalid solution id"

#list exceptions  
except IOError:
    #logging error
    logging.error('No such file or directory. Please try again')

现在你有第二个except IOError。据我所知,如果在IOError子句中引发try,则只会触发第一个异常处理程序。所以你永远不会看到Solution id is invalid消息。你的意思是在try

的正文中提出(并捕获)一个不同的例外
except IOError:
    print("Solution id is invalid. Please check the number and try again")
    #logging error
    logging.error('Solution id is invalid. Please check the number and try again') 

答案 1 :(得分:0)

我不完全确定你的问题是什么。但是在你的代码中你有

#if no object is found, run error
assert(sys.argv[1] != None), "object does not exist"

如果在命令行中指定了某些内容(遵循脚本名称),那么sys.argv[1]肯定会是某种东西,而不是None;它将是一个字符串。您的测试不应该反对从配置或类似内容中读取的某些字符串匹配吗?

或者,如果要测试命令行中是否有某些内容,请使用len()

if len(argv) < 2:
    print('Bad usage, you need to state the solution_id ' +
          'and input_file on the commandline')