Solved

Simple Python Syntax Problem

Posted on 2010-08-23
1
1,494 Views
Last Modified: 2012-05-10
Hi the following code gives me the error below.  I have never messed with python so any input would be appreciated.

Thanks!

includes# ./lllog.py
Traceback (most recent call last):
  File "./lllog.py", line 45, in <module>
    main(sys.argv[1:])
  File "./lllog.py", line 18, in main
    except getopt.GetoptError:
NameError: global name 'getopt' is not defined

#!/usr/bin/python
# import MySQL module
import MySQLdb
# import sys module for argument processing etc
import sys

def main(argv):

    fileName = "unknown"
    hfJobId = 444
    message = "hylafax update"
    type = "FAX"
    sendto = 555
    action = "update"

    try:
        opts, args = getopt.getopt(argv, "ha:", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
       if opt in ("-h", "--help"):
         usage()
         sys.exit()
       elif opt == '-a':
         action = arg

    print "action arg is", action

  # connect
    db = MySQLdb.connect(host="172.16.1.90", user="hylafax", passwd="pass",
    db="pass")

  # create a cursor
    cursor = db.cursor()
  # execute SQL statement
    cursor.execute("""INSERT INTO lablynk_send_log (fileName, hfJobId, message, type, sendto) VALUES ("test",
    "test","test", "FAX", "test")""")

  # get ID of last inserted record
    print "ID of inserted record is ", int(cursor.insert_id())

if __name__ == "__main__":
    main(sys.argv[1:])

def usage():
    print "build usage messages"

Open in new window

0
Comment
Question by:sean-keys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 13

Accepted Solution

by:
Superdave earned 500 total points
ID: 33506125
you should put a
import getopt
at the top somewhere.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question