narsonalin
narsonalin

Reputation: 79

os.path.exists works with an integer

Today, I was writing a code that checks if a file exists before doing anything with it. To do so, I use

os.path.exists( filename )

By mistake, I gave to filename an integer value instead of a string value. E.g.

os.path.exists( 15 )

To my great surprise, it did not raised a TypeError but returned True (it actually returned True for any integer I tried).

Why does it work and what meaning this result have ?

Upvotes: 3

Views: 59

Answers (1)

Barmar
Barmar

Reputation: 781028

From the documentation

Changed in version 3.3: path can now be an integer: True is returned if it is an open file descriptor, False otherwise.

When I try it, it only returns True for a couple of low numbers:

>>> [os.path.exists(n) for n in range(50)]
[True, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False]

Upvotes: 3

Related Questions