exception just to avoid output error (newbie)

James Stroud jstroud at mbi.ucla.edu
Wed Apr 18 05:24:07 EDT 2007


Flyzone wrote:
> Hi
> I'm trying to delete some files, but i get an exception error if the
> file don't exist.
> I can use an {if fileexist(file) then delete}, but it will get cpu
> time.
> So i catch the exception with:
>        try: os.remove(filename)
>        except EnvironmentError: error=1
> If i just want to avoid the output error, is this the right way to
> write the code?
> I don't care of get if is in error, but an "except EvironmetError"
> without the ":" will give me a sytax error.
> Am I too much complicated? :-)
> 
> Thanks in advance
> 

You got it! Not to complicated--just right. I would make the tiniest of 
changes for style and speed:

try:
   os.remove(filename)
except EnvironmentError:
   pass

But what you have already is fine.

James



More information about the Python-list mailing list