getopt code NameError exception on logTail.py

MRAB python at mrabarnett.plus.com
Fri Jul 17 14:08:16 EDT 2009


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! :-)



More information about the Python-list mailing list