getopt code NameError exception on logTail.py

LoD MoD lodmod.dod at gmail.com
Fri Jul 17 13:20:43 EDT 2009


I am having trouble extending my option parsing.

Any help is appreciated:


import sys

import getopt

import time


def tail(file):

    while 1:

        where = file.tell()

        line = file.readline()

        if not line:

            time.sleep(1)

            file.seek(where)

        else:

            print line, # already has newline


def main():



    # parse command line options

    try:

        opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", "filename="
])

    except getopt.error, msg:

        print msg

        print "for help use --help"

        sys.exit(2)

    # process options

    for o, a in opts:

        if o in ("-h", "--help"):

            print __doc__

            sys.exit(0)

        if o in ("-f", "--filename"):

            print "Parsing F argument"

            file = open(filename, 'r')

            print file






    # process arguments

    for arg in args:

        process(arg) # process() is defined elsewhere


if __name__ == "__main__":

    main()

Yields this error:

localhost:src gsery$ python logTail.py  /var/log/system.log
Traceback (most recent call last):
  File "logTail.py", line 52, in <module>
    main()
  File "logTail.py", line 49, in main
    process(arg) # process() is defined elsewhere
NameError: global name 'process' is not defined
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090717/ef10e2b8/attachment.html>


More information about the Python-list mailing list