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

James Porter
  • 119
  • 2
  • 12

3 Answers3

1

I don't know what you mean by OSError Errno 2, but what ever error you want you can handle in if and ignore all the others with pass statement

try:
    folder = selection
    myFunction(folder)
except Exception as e:
    if "I/O operation failed" in e.__doc__:
        print "bingo"
    else:
        pass
mid
  • 204
  • 2
  • 12
0

Use os.path.isdir(folder)

Python - isdir

Conans
  • 461
  • 1
  • 4
  • 14
0

Another way and more explicit would be:

try:
    os.remove(filepath)
except OSError, e:
    print e

However, depending on the frequency the Exception is called this method can impact speed.

user1767754
  • 23,311
  • 18
  • 141
  • 164