[Tutor] checking if file exists

Blake Winton bwinton at latte.ca
Thu Oct 14 17:52:16 CEST 2004


Mark Kels wrote:
> How can I check if file exists in a folder ?
> I tryed to do it with an if statment, but I cant use the error as a
> boolean expression...
> Any suggestions ?

 >>> import os.path
 >>> os.path.exists( r'c:\program files\python\python.exe' )
True
 >>> os.path.exists( r'c:\program files\python\pythin.exe' )
False

(I don't normally just give people the answers, but in this case I don't 
think there's anything significant to be learned.  Maybe "check the 
standard library", but that's a bit of a stretch to turn into a lesson.)

Later,
Blake.



More information about the Tutor mailing list