James Porter James Porter - 6 months ago 31
Python Question

Python: try/except OSerror errno 2

I'm writing a script where the user selects a directory and hits ok.

That I've sussed pretty easily, but I'm trying to sort error handling side, but it's not going to plan.

Essentially, I want to continue the script unless OSError Errno 2 is called.

at the moment, I have:
import IOError
...
...

if ok==1:
try:
folder = selection
myFunction(folder)
except IOError:
print "Select a folder, not a file"


I've tried using except without IOError, but that causes problems elsewhere in the script (In a different function completely)

Anyone have a suggestions?

Thanks

Answer

Use os.path.isdir(folder)

Python - isdir

Comments