Solved

Simple Python Syntax Problem

Posted on 2010-08-23
1
1,394 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python output problem 10 54
How to generate square thumbnail using perl 13 85
Python error - Need Help 12 107
Python -- Read from csv for sending mail 9 44
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

789 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