Why exception from os.path.exists()?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 3 01:48:32 EDT 2018


On Sun, 03 Jun 2018 10:38:34 +1000, Chris Angelico wrote:

> Let's just rewind this subthread a little bit. YOU said that the
> behaviour of os.path.exists on Unix systems should be "return False for
> invalid things" on the basis that the Windows invalid paths return
> False. Remember? 

No, invalid paths on Linux return False too:

py> os.path.exists("")
False


I can make a VFAT partition under Linux:

[steve at ando ~]$ dd if=/dev/zero of=fat.fs bs=1024 count=48
48+0 records in
48+0 records out
49152 bytes (49 kB) copied, 0.0149677 seconds, 3.3 MB/s
[steve at ando ~]$ /sbin/mkfs.vfat fat.fs
mkfs.vfat 2.11 (12 Mar 2005)
[steve at ando ~]$ mkdir dos
[steve at ando ~]$ sudo mount -o loop fat.fs ./dos
[sudo] password for steve:


I can write to it (as root), but not all file names are valid:

[steve at ando ~]$ sudo touch ./dos/foo
[steve at ando ~]$ sudo touch ./dos/"foo?"
touch: setting times of `./dos/foo?': No such file or directory
[steve at ando ~]$ ls ./dos
foo

And even though I'm using Linux, I get the right answer, legal file name 
or not legal file name.


[steve at ando ~]$ python3.5 -c "import os; \
> print(os.path.exists('./dos/foo'))"
True
[steve at ando ~]$ python3.5 -c "import os; \
> print(os.path.exists('./dos/foo?'))"
False



> I said that Windows isn't POSIX,

And I said, as a by-the-by, that technically Windows is POSIX compliant, 
for a very pedantically true but dubious in practice value of compliant.


> and pointed out just a couple of ways in which, to a programmer, Windows
> behaves very differently to POSIX-compliant systems.

Is that Windows out of the box, or Windows with the POSIX subsystem 
installed and active?

You keep talking about "POSIX-compliant", but POSIX is a family of 
standards. A system can be compliant with one POSIX standard without 
being compliant to the others.

And ironically, neither Linux, OpenBSD, FreeBSD nor Darwin are fully 
POSIX compliant, merely "mostly" compliant. (Or at least, they haven't 
been certified as such.)

Not that it matters much in practice.


In any case, the minutia of POSIX versus Windows, the availability of 
drive letters and signals etc are utterly irrelevant to the question of 
what os.path.exists should do.

Just as it ought to be utterly irrelevant that on Linux native C strings 
are null terminated.



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list