ealeon
ealeon

Reputation: 12462

python os module raise errors other than OSError?

http://docs.python.org/2/library/os.html

"Note All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system."

So all functions that starts with os. in front of them i.e. (os.chown and os.listdir) will only raise OSError?

Are there any cases where they would raise other errors such as IOError or whatnot? If so, could you give me some example?

P.S. I am asking this question because the website lists "some" cases but I am not sure if these "some" are the only cases associated with the os module.

Upvotes: 0

Views: 717

Answers (3)

DanielB
DanielB

Reputation: 2958

OSError will only be raised, as quoted, "in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system."

So, for example, try os.chdir(42)

>>> os.chdir(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be string, not int

TypeError - because the argument doesn't have the correct type (...other arguments that have the correct type...)

OSError means that the arguments were not accepted by the Operating System

So the answer is yes, os can raise other types of errors.

Upvotes: 3

LearningC
LearningC

Reputation: 3162

check this link, a link , here it gives IOError, even if you dont use with-as construct.
these error types or exceptions indicate the type of problem that is reason for the call failure.so different types of exceptions are posible.even with os module methods.they throw exceptions to indicate the type of problem.so may not be OSError only.

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304375

IOError is possible if there is a problem accessing the location - like someone unplugged the USB drive

MemoryError can pop up nearly anywhere if you run out of memory

Upvotes: 2

Related Questions