exception just to avoid output error (newbie)

F

Flyzone

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
 
J

James Stroud

Flyzone said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top