Portable filename comparison

0 views
Skip to first unread message

Ken Dyck

unread,
Jan 15, 2004, 8:35:44 AM1/15/04
to
Is there a way with Python 2.2 to portably test the equivalence of two
filenames?

The os.path.samefile function seems to do what I want, but it is only
available on Unix and Mac. I need something that works on Windows.

The os.path.normcase and os.path.normpath functions are available on all
platforms, but don't quite get me there. For example, on Windows

>>> os.path.normcase(os.path.normpath("C:\Program Files"))
'c:\\program files'
>>> os.path.normcase(os.path.normpath("C:\PROGRA~1"))
'c:\\progra~1'

I suppose I could find a way to call into the Win32 API directly, but I'd
prefer to use a portable solution if I can find one.

Any suggestions?

Ken


Peter Hansen

unread,
Jan 15, 2004, 11:23:42 AM1/15/04
to

I believe you need to make your own, since the whole concept of thus
mungled long names on Windows is definitely not portable.

Here's a snippet that might make this seem much easier:

>>> def expand(shortname):
... from win32api import GetLongPathName
... return GetLongPathName(shortname)
...
>>> expand('c:\\progra~1')
'c:\\Program Files'

-Peter

Reply all
Reply to author
Forward
0 new messages