Comment 2 for bug 172861

Revision history for this message
Alexander Belchenko (bialix) wrote : Re: [Bug 172861] Re: add: accepts aliases due to case insensitivity

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John A Meinel пишет:
>
> The other problem, is that I don't know of any way to get the real name of a file. You could use "os.listdir(os.path.dirname(path))" and then search through it for possible names, but you don't know if foO is there because of a rename, or if it is actually a different file.
> It would be nice if you could do:
>
> st = os.stat('foo')
> and have "st" have a st_name or some other property that gives you the exact name on disk.
>
> Does anyone know if that is possible?

I know. Recently I dig through MSDN and found this way to determine real filename without
using os.listdir (it's too expensive in Python). You need pywin32 library for this, or
writing C-extension. Using ctypes is also possible but it's too verbose in Python.

Here the actual code:

import win32file
names_list = win32file.FindFilesW(path_in_question) # returns list of WIN32_FIND_DATA structs
# if path_in_question contains wildcard characters * or ? then we get list of al matching files,
# like with glob function
real_name = names_list[0][8]

Here is example running in bzr.dev tree:

In [4]: import win32file

In [5]: names_list = win32file.FindFilesW('BzR')

In [6]: names_list[0][8]
Out[6]: u'bzr'

In [7]: len(names_list)
Out[7]: 1
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHTwwMzYr338mxwCURAi9NAJ42y5oTAb8nLnAPWUO/DkM2nM7LAwCeL89s
q28tkBoR6W5ydoG3Ynrl7q0=
=VhtB
-----END PGP SIGNATURE-----