Why exception from os.path.exists()?

Bev in TX countryone77 at gmail.com
Sun Jun 10 10:45:06 EDT 2018



> On Jun 10, 2018, at 5:49 AM, Peter J. Holzer <hjp-python at hjp.at> wrote:
> 
> On 2018-06-07 12:47:15 +0000, Steven D'Aprano wrote:
>> But it doesn't do that. "Pathnames cannot contain NUL" is a falsehood 
>> that programmers wrongly believe about paths. HFS Plus and Apple File 
>> System support NULs in paths.
> [...]
>> But in the spirit of compromise, okay, let's ignore the existence of file 
>> systems like HFS which allow NUL. Apart from Mac users, who uses them 
>> anyway? Let's pretend that every file system in existence, now and into 
>> the future, will prohibit NULs in paths.
> 
> Could you (or anybody else who owns a Mac) please do the following:
> 
> * Create an empty directory
> * In this directory, create two files:
>  * One with an embedded \0 in the file name

I don’t know how to do this.  I can’t enter a Nul in Finder.  Bash silently converts it to a zero when using it as a file name.  C considers the previous character the end of the file name.  Python considers it an error.

>  * One with an embedded / in the file name

This is easily done in Finder, where I created a folder named "my/slash”.  
When I list it at the command line in Terminal, this shows up as "my:slash”, with the slash shown as a colon.  
If I create a file with a colon in its name at the command line, that file name acts the same way:

$ touch ‘my:colon"
$ ls
my:colon
my:slash

In Finder they both display as:
my/colon
my/slash

However, if you use Finder’s “Copy item as Pathname” option, then you will again see the colon.  

/Users/bev/Training/myPython/pygroup/files/my:colon
/Users/bev/Training/myPython/pygroup/files/my:slash

But if you paste that folder’s name in Finder’s “Go to Folder” option, it converts it to the following, and goes to that folder:

/Users/bev/Training/myPython/pygroup/files/my/slash/slash

So we can see three (3) separate behaviors for the same folder in Finder.  This is because at some higher level, Apple’s file systems reserve the colon for the path separator, in stead of the slash.  I know that AppleScript does use the colon as a path separator.  However at a lower level, macOS still uses the slash, rather than colon, as the path name separator.  IMO, this is confusing.  Similarly in Finder file names are case insensitive, but case preserving.  At the command line they are still case sensitive.  Note that it’s possible to format a disk with a fully case sensitive file system, but that’s not the default.

Please note that I am not an Apple HFS+ or AFS file system expert, by any stretch of the imagination.  So please excuse me if I did’t state things perfectly.

> * Compile and run this C program in the directory and post the output:
> 
>    #include <stdio.h>
>    #include <sys/types.h>
>    #include <dirent.h>
> 
>    int main(void) {
>        DIR *dp;
>        struct dirent *de;
>        char *p;
> 
>        dp = opendir(".");
>        while ((de = readdir(dp)) != NULL) {
>            printf("%ld -", (long)de->d_ino);
>            for (p = de->d_name; *p; p++) {
>                printf(" %02x", (unsigned char)*p);
>            }
>            printf("\n");
>        }
>        return 0;
>    }

I added printing the file name.  As suspected, the “slash” is a colon:

. - 56096374 - 2e
.. - 56095464 - 2e 2e
.DS_Store - 56109197 - 2e 44 53 5f 53 74 6f 72 65
my:colon - 56095933 - 6d 79 3a 63 6f 6c 6f 6e
my:slash - 56095521 - 6d 79 3a 73 6c 61 73 68

> Bonuspoints for doing this on an USB stick and then mounting the USB
> stick on a Linux system and posting the output there as well.
> 
Sorry, I don’t have Linux, but I suspect it’s the same as the macOS command line.

> I'm really curious how MacOS maps those characters in the POSIX API.
> 
>        hp

Bev in TX






More information about the Python-list mailing list