simple: test if file exists

Donn Cave donn at u.washington.edu
Wed Aug 30 18:43:38 EDT 2000


Quoth Kirby Urner <urner at alumni.princeton.edu>:
| 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 don't know, this appears to substitute error prediction for
error trapping.  I'm no Windows whiz, but the UNIX version
of system() will indeed return an integer value that indicates
whether the command supposedly worked.  It's usually shifted
8 bits up, if you want the particular exit value, but the main
point is that 0 is good, anything else is bad.

Python obviously doesn't raise an exception on the exit, and
that's good because in practice a 0 exit doesn't reliably tell
you that your command worked, nor does the inverse apply.  But
you can certainly do that yourself, should give it a try anyway.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list