简体   繁体   English

在Python中为命令行上的错误用户ID创建异常

[英]Creating an Exception in Python for improper user id on command line

The user needs to enter a path to their file along with an id that is on every line in their file. 用户需要输入文件的路径以及文件每一行中的ID。 I have created an exception for a wrong file and I want to do the same if the id that is typed doesn't match what's in the file. 我为错误的文件创建了一个例外,如果键入的ID与文件中的ID不匹配,我想做同样的事情。 How do I do this? 我该怎么做呢?

import ConfigParser, os
import csv
import sys, getopt
import time
import datetime

from sys import argv
script, solutionId, input_file = argv

def check_all(f):
    print f.read()

def print_a_line(line_count, f):
    print line_count, f.readline()

try:
    current_file = open(input_file)
    check_all(current_file)
except IOError, e:
    print '%s, please try again.' % e

Well just declare your exception: 好吧,宣布您的例外情况:

class WrongId(Exception): pass

And raise it when checking the file integrity: 并在检查文件完整性时提高它:

if not check_id_in_file(input_id):
    raise WrongId()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM