simple: test if file exists

Kirby Urner urner at alumni.princeton.edu
Wed Aug 30 17:14:51 EDT 2000


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.





More information about the Python-list mailing list