simple: test if file exists

Bjorn Pettersen bjorn at roguewave.com
Wed Aug 30 17:40:11 EDT 2000


Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import os
>>> print os.path.exists.__doc__
Test whether a path exists
>>> os.path.exists(r'c:\autoexec.bat')
1
>>>

Kirby Urner wrote:
> 
> I invoke the ray tracer Povray from within a class.
> The method currently looks like this:
> 
>     def render(self):
> 
>         if sys.platform == 'win32':
>             # we're in Windows
>             os.system(self.wincomm+" +I"+self.filename)
> 
>         if sys.platform == 'linux-i386':
>             # we're in Linux
>             print "Rendering... (this will take some time)"
>             os.system(self.linuxcomm+" +I"+self.filename)
> 
> The self.wincomm variable contains the path/filename and
> parameter switches relevant to that platform e.g.
> 
>     wincomm   = "g:\\povray\\bin\\pvengine /NR /EXIT "
> 
> ditto for Linux.
> 
> I thought I'd improve this code with some error trapping.
> In testing, I deliberately stored a bogus path/filename
> to wincomm, and used try:  except: around the os.system()
> call, expecting it to fail.  But it succeeded (!) --
> doesn't really matter to os that the DOS window reported
> 'Bad Command or File Name' or whatever (that's not a
> Python problem, after all).
> 
> So I guess what I need to test for is the existence of
> the file I'm wanting to boot, e.g. g:\\povray\\bin\\pvengine.
> 
> I've been pawing through the docs (nice to have the Help
> menu in IDLE actually boot the browser in Windows in 1.6b
> -- s'been a bug), but haven't yet found the module which
> contains such a cross-platform 'file existence' test.
> 
> Kirby
> 
> Pls cc:  urner at alumni.princeton.edu , gracias and TIA.
> 
> --
> http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list