Strange os.path.exists() behaviour

P

Pierre Quentel

os.path.exists(path) returns True if "path" exists

But on Windows it also returns True for "path" followed by any number of
dots :

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Is there a reason for this ? Is there a test that returns True only for
the really existing path ?

Pierre
 
J

Jeff Epler

Pierre said:
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/d...n-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, _access(argv, 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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCzDn4Jd01MZaTXX0RAuCEAJ9fcQp7EL7uXEnCnDD3iBkpFih5/gCfQ9Jd
uglZQylJ9n2ChXMUinFaACk=
=jFuL
-----END PGP SIGNATURE-----
 
S

Sion Arrowsmith

Jeff Epler said:
Pierre said:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on wi=
n32
^^^^=
^^^
Here's the bug. You're using Windows. It's a filesystem, but not as we kn=
ow it...
[ ... ]
As you can see, not only does Windows think that "exist...." exists, but it=
can
successfully "type" its contents too!

The "reason" for this behaviour is that the concept of a file
extension is firmly embedded in the operating system. exists is a
file with no extension, as is exists. (no, it doesn't make a
distinction between "no extension" and "an empty extension").
From there it's not too great a leap of induction to suppose that
exists.... is the same file called exists with no extension. It's
not unlike being able to stick an arbitrary number of /s onto the
end of a directory name on a sane OS/filesystem.
 
C

Casey Hawthorne

Does this work differently under other platforms?

Pierre Quentel said:
os.path.exists(path) returns True if "path" exists

But on Windows it also returns True for "path" followed by any number of
dots :

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Is there a reason for this ? Is there a test that returns True only for
the really existing path ?

Pierre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top