使用标准库生成 8.3 文件名

发布于 2025-01-10 16:29:18 字数 1422 浏览 0 评论 0原文

我想生成一个没有模块 win32apictypes 的 8.3 文件名(在 DOS/FAT 上使用)(都不适合我的配置)。

目前,代码是这样的:

def short_names(names):
    names2 = []
    for i in names:
        append_tilde = True
        
        b = set(".\"/\\[]:;=, ") # ."/\[]:;=,[space] (forbidden chars)
        old = i
        
        for char in b:
            i = i.replace(char, "")
        
        if i == old: append_tilde = False
        
        name_parts = i.split(sep=".")
        name = ''.join(name_parts[0:len(name_parts)-1])
        extension = name_parts[-1][0:3]
        
        if len(name) > 6:
            name = name[0:6]
            append_tilde = True
        
        if append_tilde:
            for j in range(1,10):
                if name.upper()+"~"+str(j) not in names2:
                    names2.append(name.upper() + "~" + str(j))
                    break
                    
        
    return names2

但它只返回“~1”部分,而不是6个字符的部分加上“~1”。

对于示例输入:

["Program Files", "ProgramData", "Programme", "Documents and Settings", "Dokumente und Einstellungen"] 它返回 ['~1', '~2', '~3']

预期返回值:

["PROGRA~1", "PROGRA~2", "PROGRA~3", " DOCUME~1", "DOKUME~1"]

Python 版本:Python 3.10.1 (v3.10.1:2cd268a3a9,12 月 6 日2021, 14:28:59) [Clang 13.0.0 (clang-1300.0.29.3)] 达尔文

I would like to generate an 8.3 filename (as used on DOS/FAT) without the modules win32api or ctypes (neither works with my configuration).

Currently, the code is this:

def short_names(names):
    names2 = []
    for i in names:
        append_tilde = True
        
        b = set(".\"/\\[]:;=, ") # ."/\[]:;=,[space] (forbidden chars)
        old = i
        
        for char in b:
            i = i.replace(char, "")
        
        if i == old: append_tilde = False
        
        name_parts = i.split(sep=".")
        name = ''.join(name_parts[0:len(name_parts)-1])
        extension = name_parts[-1][0:3]
        
        if len(name) > 6:
            name = name[0:6]
            append_tilde = True
        
        if append_tilde:
            for j in range(1,10):
                if name.upper()+"~"+str(j) not in names2:
                    names2.append(name.upper() + "~" + str(j))
                    break
                    
        
    return names2

But it returns the "~1" part only, not the 6-character part plus "~1".

For the example input:

["Program Files", "ProgramData", "Programme", "Documents and Settings", "Dokumente und Einstellungen"]
it returns
['~1', '~2', '~3']

Intended return value:

["PROGRA~1", "PROGRA~2", "PROGRA~3", "DOCUME~1", "DOKUME~1"]

Python version: Python 3.10.1 (v3.10.1:2cd268a3a9, Dec 6 2021, 14:28:59) [Clang 13.0.0 (clang-1300.0.29.3)] on darwin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み零 2025-01-17 16:29:18

问题在于您尝试将文件名拆分为基本部分和扩展名的方式。

如果您对其中没有 . 的字符串调用 split('.'),您将得到一个包含单个元素的列表 - 您的原始字符串。这意味着 name_parts[0:len(name_parts)-1]name_parts[0:0] 相同,后者是一个空列表。您将 name 设置为空字符串,而 extension 设置为整个文件名的前 3 个字符。

您需要检测文件名中没有 . 的情况并进行不同的处理。

    name_parts = i.split(sep=".")
    if len(name_parts) <= 1:
        name = i
        extension = ''
    else:
        name = ''.join(name_parts[0:len(name_parts)-1])
        extension = name_parts[-1][0:3]

PS Python 有一些工具可以让这变得更容易。查看 os.path 或 < a href="https://docs.python.org/3/library/pathlib.html#module-pathlib" rel="nofollow noreferrer">pathlib

The problem is in the way you try to split a filename into a base part and an extension.

If you call split('.') on a string that doesn't have a . in it, you get back a list with a single element - your original string. This means that name_parts[0:len(name_parts)-1] is the same as name_parts[0:0] which is an empty list. You're setting name to an empty string, while extension is set to the first 3 characters of the entire file name.

You need to detect the case where there was no . in the filename and treat it differently.

    name_parts = i.split(sep=".")
    if len(name_parts) <= 1:
        name = i
        extension = ''
    else:
        name = ''.join(name_parts[0:len(name_parts)-1])
        extension = name_parts[-1][0:3]

P.S. Python has some facilities to make this easier. Check out os.path or pathlib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文