getopt code NameError exception on logTail.py

LoD MoD lodmod.dod at gmail.com
Fri Jul 17 20:32:50 CEST 2009


In this instance the trackback was somewhat unhelpful.There problem was
here:

           file = open(filename, 'r')

should be

           file = open(a, 'r')

args should be passed within the getopt riff

On Fri, Jul 17, 2009 at 11:08 AM, MRAB <python at mrabarnett.plus.com> wrote:

> LoD MoD wrote:
>
>> 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
>>
>>  The trackback tells you what's wrong: you haven't defined 'process'. The
> comment says it's defined elsewhere, but neither I nor Python can see
> it! :-)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090717/644d3ab1/attachment.html>


More information about the Python-list mailing list