I\'m new in python programming. When i try running a simple python script i get error like this in my terminal
root@bt:/tmp# python code.py
Traceback (most recen
Your except
clause will catch any error in any line of code in the try
block. If you don't specify enough arguments on the command line, the line host = str(sys.argv[1])
will fail, leaving host
unassigned, which then causes the error you are seeing when you try to print
it.
You should take most of the code out of your try
block, really, and/or create multiple try
blocks that catch errors in much smaller chunks of code. Furthermore, you should specify the actual exception type you want to handle with each except
instead of trying to handle all of them. Bare except:
catches things you probably don't want caught, such as KeyboardInterrupt
and SystemExit
. If you must catch most exceptions, use except Exception:
instead of just except:
.