[python-win32] 'exit is not defined'

sharpblade sharpblade1 at gmail.com
Fri Jun 3 21:14:51 CEST 2011


Why do you need a finally block there at all?
Just terminate on the exception, there is no point in setting it to
None then checking if its none.

~Tom

On Fri, Jun 3, 2011 at 9:33 AM, Jacob Kruger <jacobk at mailzone.co.za> wrote:
> Sorry, but replying on list now as well.
>
> Either way, seems that while did have the import statement higher up in that file for the sys module, maybe there was something interfering with that, since when changed that statement to sys.exit() it's now working without any issues.
>
> And, secondly, the reason I'm using tabs to handle indentation is partly since the editor I specifically am using for python source editing prefers that for use by VI/blind guys, and that's why it suits me, but anyway...<smile>
>
> Jacob Kruger
> Blind Biker
> Skype: BlindZA
> '...fate had broken his body, but not his spirit...'
>
>
> ----- Original Message ---------------
>
> Subject: Re: [python-win32] 'exit is not defined'
>   From: "Amaury Forgeot d'Arc" <amauryfa at gmail.com>
>   Date: Fri, 3 Jun 2011 09:43:47 +0200
>     To: Jacob Kruger <jacobk at mailzone.co.za>
>
>>Hi,
>>
>>2011/6/3 Jacob Kruger <jacobk at mailzone.co.za>:
>>> What I'm doing is trying to open a text file, but if it's not there, then I tell the code to exit(), and while it works when running the source code using the python executable, after I have run the code through py2exe, and then try testing it, I get the following message:
>>>
>>> Traceback (most recent call last):
>>> File "mapData.py", line 39, in <module>
>>> NameError: name 'exit' is not defined
>>>
>>> The following is the actual little bit of code that's generating this after being py2exe'ed - the last line is the line number being mentioned above:
>>>
>>> try:
>>>        fData = open(sMapName + ".txt", "r")
>>> except:
>>>        fData = None
>>> finally:
>>>        if not bool(fData): print("invalid map name - " + sMapName + ".txt"); exit()
>>>
>>>
>>> Any thoughts/ideas/alternative workarounds?
>>
>>"exit" is in the sys module, you shoud have an "import sys" somewhere,
>>and then calll "sys.exit()".
>>Maybe you tested your script from some IDE which already does "from
>>sys import exit" for you?
>>
>>By the way, your code could be improved in several ways:
>>- don't use tabs for indentation, use spaces instead.
>>- "if not bool(fData)" could be rewritten as "if not fData"
>>- open() always return a file object, so the logic could be:
>>
>>import sys
>>
>>try:
>>    fData = open(sMapName + ".txt", "r")
>>except IOError:
>>    print("invalid map name - " + sMapName + ".txt")
>>    sys.exit()
>>
>>I hope I did not spoil all the fun - coding with Python is fun!
>>
>>--
>>Amaury Forgeot d'Arc
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>


More information about the python-win32 mailing list