如何在Python中获取Windows短文件名?

15

我需要从Python代码中确定Windows短文件名。为此,我可以使用win32api找到解决方案。

import win32api
long_file_name='C:\Program Files\I am a file'
short_file_name=win32api.GetShortPathName(long_file_name)

参考资料:http://blog.lowkster.com/2008/10/spaces-in-directory-names-i-really-love.html

不幸的是,我需要安装 pywin32 或者 ActivePython,但在我的情况下这是不可能的。

还可以参考 SO 上的内容:

获取 Python 中的短路径:Getting short path in python


请注意,在NTFS中生成短文件名是可选的,并建议在具有数千个文件的目录的系统上禁用它,因为它会显著减慢访问速度,并且ReFS和exFAT根本不支持短文件名。有各种更好的方法来解决经典DOS“MAX_PATH”限制的问题,例如“\?\”设备路径、subst/映射驱动器、挂载点(联接)和符号链接。 - Eryk Sun
1个回答

22

你可以使用ctypes。根据MSDN文档GetShortPathNameKERNEL32.DLL中。请注意,真正的函数是GetShortPathNameW用于宽字(Unicode)符号和GetShortPathNameA用于单字节字符。由于宽字符更常用,因此我们将使用该版本。首先,根据文档设置原型:

import ctypes
from ctypes import wintypes
_GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW
_GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
_GetShortPathNameW.restype = wintypes.DWORD

GetShortPathName 用法:首先调用函数不带目标缓冲区,它将返回所需的缓冲区字符数。然后使用该大小调用函数一次。如果由于 TOCTTOU 问题,返回值仍较大,请继续尝试直到成功为止。

def get_short_path_name(long_name):
    """
    Gets the short path name of a given long path.
    https://dev59.com/gmAg5IYBdhLWcg3wbqh4#23598461
    """
    output_buf_size = 0
    while True:
        output_buf = ctypes.create_unicode_buffer(output_buf_size)
        needed = _GetShortPathNameW(long_name, output_buf, output_buf_size)
        if output_buf_size >= needed:
            return output_buf.value
        else:
            output_buf_size = needed

嗨,这段代码对我来说似乎非常完美,除了最后一个文件夹。你有什么想法吗?这是路径 C:\data\SIEA\_0 Mise à niveau des boites\Zone NRA de Ferney\Ornex 04-05\APD_e\06_Fichier Adresse - IPE - GuiOm Clair
2
调用可能会失败。因此请使用 kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) 代替 ctypes.windll.kernel32。然后,如果 needed == 0,通过 raise ctypes.WinError(ctypes.get_last_error()) 抛出异常。 - Eryk Sun
非常方便!我唯一的建议是从 output_buf_size=len(long_name) 开始,这将使它几乎在第一次尝试时就能正常工作,因为 short_path 很少比输入路径更长。 - Dan Lenski
补充一下 @ErykSun 的评论:稍微简写一下,您可以执行 if needed == 0: raise ctypes.WinError() 这样就会为您执行 GetLastError()。我也没有必要这样指定 use_last_error=True,但是可能因人而异。 - Colin Atkinson
很遗憾,出现了错误:AttributeError: module 'ctypes' has no attribute 'windll' - Oliver

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接