Strange os.path.exists() behaviour

Jeff Epler jepler at unpythonic.net
Wed Jul 6 16:07:23 EDT 2005


Pierre wrote:
> Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
                                                                       ^^^^^^^
Here's the bug.  You're using Windows.  It's a filesystem, but not as we know it...

Anyway, You are getting exactly what the low-level Windows APIs return.

Here's a small "C" program.  It prints "0" next to the filename if the
file exists, -1 otherwise, as described at
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__access.2c_._waccess.asp

int main(int argc, char **argv) {
    int i;
    for(i=1; i<argc; i++) {
        printf("%20s: %d\n", argv[i], _access(argv[i], 0)); 
    }
}

I compiled it with "i386-mingw32msvc-gcc -o a.exe exist.c"

Here's an example session with it:
    C:\TMP\example>dir
     Volume in drive C has no label.
     Volume Serial Number is 171D-4D2A

     Directory of C:\TMP\example

    07/06/05  03:04p        <DIR>          .
    07/06/05  03:04p        <DIR>          ..
    07/06/05  03:05p                     3 exist
                   3 File(s)              3 bytes

    C:\TMP\example>x:a.exe exist exist. exist.... nonexist nonexist. nonexist...
                   exist: 0
                  exist.: 0
               exist....: 0
                nonexist: -1
               nonexist.: -1
             nonexist...: -1

    C:\TMP\example>type nonexist....
    The system cannot find the file specified.

    C:\TMP\example>type exist....


    C:\TMP\example>

As you can see, not only does Windows think that "exist...." exists, but it can
successfully "type" its contents too!

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050706/091a0fc8/attachment.sig>


More information about the Python-list mailing list