Why exception from os.path.exists()?

Chris Angelico rosuav at gmail.com
Fri Jun 1 19:56:58 EDT 2018


On Sat, Jun 2, 2018 at 9:37 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Thu, 31 May 2018 17:43:28 +0000, Grant Edwards wrote:
>
>> Except on the platform in quetion filenames _don't_ contain an embedded
>> \0.  What was passed was _not_ a path/filename.
>
> "/wibble/rubbish/nobodyexpectsthespanishinquistion" is not a pathname on
> my system either, and os.path.exists() returns False for that. As it is
> supposed to.
>
> I'd be willing to bet that:
>
> import secrets  # Python 3.6+
> s = "/" + secrets.token_hex(1024) + "/spam"
>
> is not a pathname on any computer in the world. (If it is even legal.)
> And yet os.path.exists(s) returns False.

With both of these, the path cannot exist because its first component
does not exist. Absent a /wibble on your system, the entire long path
is unable to exist. That is a natural consequence of the hierarchical
structure of file systems. I'm fairly sure 2KB of path is valid on all
major OSes today, which means that it's exactly the same as /wibble -
the first component doesn't exist, ergo the path doesn't exist.

> The maximum number of file components under POSIX is (I believe) 256. And
> yet:
>
> py> os.path.exists("/a"*1000000)
> False
>
> "/a" by one million cannot possibly be a path under POSIX.

I can't actually find that listed anywhere. Citation needed. But
assuming you're right, POSIX is still a set of minimum requirements -
not maximums, to my knowledge. If some operating system permits longer
paths with more components, it won't be non-compliant on that basis.
So it's still plausible to ask "does this path exist", and it's
perfectly correct to look at the first "/a/" and check if there's
anything named "a" in your root directory, and return False upon
finding none. The question is sane, unlike os.path.exists([]).

ChrisA



More information about the Python-list mailing list