[Tutor] not operator

christopher.henk at allisontransmission.com christopher.henk at allisontransmission.com
Fri Sep 19 01:19:29 CEST 2008


jeremiah.jester at panasonic.aero wrote on 09/18/2008 06:12:30 PM:

> i want to check if a dir does not exist. how do i change this statement
> to evaluate is NOT existing? ==False or ! operator. Also, is sys.exit
> appropriate to use to quit out of the program?
> 
> if(os.access(target_dir, os.F_OK)):
>    print "File does not exist!"
>    sys.exit 
> 

os.access(target_dir, os.F_OK) is going to return False if the directory 
does not exist so take your pick how you want to check it.

if not os.access(target_dir, os.F_OK):

or 

if os.access(target_dir, os.F_OK)==False:

or

if os.access(target_dir, os.F_OK)!=True:


I like the first one, others like the second.

the below doesn't work in python
>>> if !(os.access(target_dir, os.F_OK)):
SyntaxError: invalid syntax


sys.exit is fine as long as you call it. 
sys.exit()
you left off the () in your sample code, and I wasn't sure if that was a 
typo or not. 
Python will not raise an error.



Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080918/b15ffdbd/attachment.htm>


More information about the Tutor mailing list