栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您可以使用

ctypes
。根据MSDN上的文档,
GetShortPathName
位于中
KERNEL32.DLL
。需要注意的是真正的功能是
GetShortPathNameW
用于
W¯¯
IDE(Unipre)字符和
GetShortPathNameA
单字节字符。由于宽字符更为通用,因此我们将使用该版本。首先,根据文档设置原型:

import ctypesfrom 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.    http://stackoverflow.com/a/23598461/200291    """    output_buf_size = 0    while True:        output_buf = ctypes.create_unipre_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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617622.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号