0

I have a script which create a temporary text file and delete after the user close the window.

The problem is that, the temporary text file may or may not be created depending on what the user does.Or sometimes the temporary text file may be deleted before the user exit. There are three possible scenario.

  1. The temporary text file is created with the name of 'tempfilename'.
  2. The temporary text file is created with the name of 'tempfilename' but deleted before the user exit.So, when trying to remove the file it raise OSError
  3. The temporary text file is not created and no variable called 'tempfilename' is created, so it raise NameError

I have tried using this code:

try:
    os.remove(str(tempfilename))
except OSError or NameError:
    pass

But it seems that it only catch the OSError only. Did i do something wrong?

4
  • 1
    Why are you catching NameError? You know that is only raised if you have a typo in your variable names, right? Commented Jul 13, 2013 at 15:38
  • @delnan: Read the question, which makes it sounds like tempfilename may not be created (presumably it is created in an if statement. I would have changed it to add tempfilename = None at the start and then do if tempfilename:, but it's not my code Commented Jul 13, 2013 at 15:44
  • @delnan i have the above code executed whenever user click on exit button. And the file name variable 'tempfilename' only created when user do specific task. And sometimes user close the script without doing anything so the variable name 'tempfilename' is not even created. It give me NameError in that case. I am a beginner so not sure if i am doing the right way though. But whatever works.. :D Commented Jul 13, 2013 at 15:46
  • @ChrisAung Always have the variable defined, initialized to some dummy value (e.g. None). Not only is this cleaner IMHO, it's also less code and you can stop catching NameError which might hide legitimate errors (e.g. typos). Commented Jul 13, 2013 at 15:50

2 Answers 2

3
try:
    os.remove(str(tempfilename))
except (OSError, NameError):
    pass
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. :) And you are a big one.@Stephan
0
tempfilename = None

# ...

if tempfilename is not None and os.path.exists(tempfilename):
    os.remove(tempfilename)

It's not good to catch NameError since it will hide other typos in your code (e.g., os.remov(…)).

Also, OSError does not always means that the file did not exist. On Windows, if the file was in use, an exception would be raised (http://docs.python.org/2/library/os.html#os.remove). In that case, you would want to see the exception so you could be aware of the issue and/or handle it another way.

Exception handlers should be kep as narrow as possible to avoid hiding unrelated errors or bugs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.