Jump to content

I want to write a python code that uses the built-in with function to open a file, but I want a ...


G+_abby Sand
 Share

Recommended Posts

I want to write a python code that uses  the built-in with function to open a file, but I want a specific command to be executed each time an exception is raised. The problem I am having is that the command I want to raise is skipped each time an exception is raised

 

 

try:

  with open(some file) as f

    s = f.read

except IOError:

    this part of the code is never executed

 

how do write the with statement so it executes.

thanks

Link to comment
Share on other sites

Regardless of environment, the last part of my statement holds true. If you are executing code that runs and then ends without calling another part of your program to execute, your code will execute and end.  If you were to say prompt a menu and wait for input, or call another program to handle the instance you caught (by say creating the missing file and then calling this function again) you would make the program continue to run and shouldn't see it exit as you have described.

Link to comment
Share on other sites

 Share

×
×
  • Create New...