Why exception from os.path.exists()?

Tim Chase python.list at tim.thechases.com
Sat Jun 2 11:19:05 EDT 2018


On 2018-06-02 00:14, Steven D'Aprano wrote:
> Since /wibble doesn't exist, neither does /wibble/a\0b
> 
> 
> py> os.path.exists("/wibble")  
> False
> py> os.path.exists("/wibble/a\0b")  
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/storage/torrents/torrents/python/Python-3.6.4/Lib/
> genericpath.py", line 19, in exists
>     os.stat(path)
> ValueError: embedded null byte
> 
> Oops.

Existence is a sketchy sort of thing.  For example, sometimes the OS
hides certain directory entries from some syscalls while allowing
visibility from others.  The following comes as on the FreeBSD system
I have at hand:

  >>> import os
  >>> '.bashrc' in os.listdir() # yes, it sees hidden dot-files
  True
  >>> '.zfs' in os.listdir() # but the OS hides .zfs/ from listings
  False
  >>> os.path.exists('.zfs') # yet it exists and can cd into it
  True
  >>> os.chdir('.zfs') # and you can chdir into it
  >>> os.listdir() # but you can't listdir in it
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  OSError: [Errno 22] Invalid argument



More information about the Python-list mailing list