使用标准库生成 8.3 文件名
我想生成一个没有模块 win32api
或 ctypes
的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您尝试将文件名拆分为基本部分和扩展名的方式。
如果您对其中没有
.
的字符串调用split('.')
,您将得到一个包含单个元素的列表 - 您的原始字符串。这意味着name_parts[0:len(name_parts)-1]
与name_parts[0:0]
相同,后者是一个空列表。您将name
设置为空字符串,而extension
设置为整个文件名的前 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 thatname_parts[0:len(name_parts)-1]
is the same asname_parts[0:0]
which is an empty list. You're settingname
to an empty string, whileextension
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.P.S. Python has some facilities to make this easier. Check out
os.path
orpathlib
.