James Porter James Porter - 1 year ago 145
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:
folder = selection
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?


Answer Source

Use os.path.isdir(folder)

Python - isdir

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download