# Python and Exception Handling IOError [closed]

I'm trying to work with exception handling by displaying "No Such File" when the file doesn't exist. I need to use a try statement.

import sys

try:
f = open("h:\\nosuchdirectory\\nosuchfile.txt","w") # do not modify this line
f.write("This is the grape file") # do not modify this line
f.close() # do not modify this line

except IOError:
print ("No Such File")


Started with:

f = open("h:\\nosuchdirectory\\nosuchfile.txt","w") # do not modify this line
f.write("This is the grape file") # do not modify this line
f.close() # do not modify this line

-

## closed as off-topic by Ruslan Osipov, syb0rg, codesparkle, Vogel612, Marco AciernoApr 12 '14 at 22:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Ruslan Osipov, syb0rg, codesparkle, Vogel612, Marco Acierno
If this question can be reworded to fit the rules in the help center, please edit the question.

From the answer given by @RuslanOsipov, it appears that your code actually does not work. You should only post working code on Code Review, otherwise, your question will be deemed off-topic and closed. You can still edit your question with code working as intended (actually printing the message when the file does not exist) if you want to keep it open, and we will be happy to review it :) –  Morwenn Apr 11 '14 at 21:41
Also, after re-reading a question this looks like an un-finished homework to me. –  Ruslan Osipov Apr 11 '14 at 21:52

This is actually an interesting question.

@RuslanOsipov has provided a solution, but technically it is vulnerable to a time-of-check vs. time-of-use race condition. In other words, there is a brief window between the isfile() call and the open() call where a things could go wrong. Depending on the application, that could even be considered a security vulnerability.

So, what to do? For the full story, read Python Issue 12760. In Python 3.3, open() gained an 'x' mode flag that makes it behave like the code in this question wants it to. For older versions of Python that lack the 'x' flag, the workaround is:

def create(file):
fd = os.open(file, os.O_EXCL | os.O_CREAT | os.O_WRONLY)
return os.fdopen(fd)

-
Ha, did not know that. Awesome. –  Ruslan Osipov Apr 12 '14 at 1:15

You will not get IOError when opening a file for writing. Writing a non-existent file will just create a new one. Here's a solution:

import os

path = "h:\\nosuchdirectory\\nosuchfile.txt"
if os.path.isfile(path):
print("No such file!")
return
f = open(path, "w") # do not modify this line
f.write("This is the grape file") # do not modify this line
f.close() # do not modify this line

-
Good catch. I did not notice the "w". That also means that the question is off-topic since the code does not work. –  Morwenn Apr 11 '14 at 21:37
@Morwenn Oh, you're right. Voted. –  Ruslan Osipov Apr 11 '14 at 21:41