根据操作系统的不同,在哪里可以找到文件名中允许的字符列表? (例如,在Linux中,字符:被允许出现在文件名中,但在Windows中不允许)


当前回答

下面是在python中清除文件名的代码。

import unicodedata

def clean_name(name, replace_space_with=None):
    """
    Remove invalid file name chars from the specified name

    :param name: the file name
    :param replace_space_with: if not none replace space with this string
    :return: a valid name for Win/Mac/Linux
    """

    # ref: https://en.wikipedia.org/wiki/Filename
    # ref: https://stackoverflow.com/questions/4814040/allowed-characters-in-filename
    # No control chars, no: /, \, ?, %, *, :, |, ", <, >

    # remove control chars
    name = ''.join(ch for ch in name if unicodedata.category(ch)[0] != 'C')

    cleaned_name = re.sub(r'[/\\?%*:|"<>]', '', name)
    if replace_space_with is not None:
        return cleaned_name.replace(' ', replace_space_with)
    return cleaned_name

其他回答

我采取了不同的方法。我不检查字符串是否只包含有效字符,而是查找无效/非法字符。

注意:我需要验证一个路径字符串,而不是一个文件名。但是如果您需要检查文件名,只需将/添加到该集合。

def check_path_validity(path: str) -> bool:
    # Check for invalid characters
    for char in set('\?%*:|"<>'):
        if char in path:
            print(f"Illegal character {char} found in path")
            return False
    return True

下面是在python中清除文件名的代码。

import unicodedata

def clean_name(name, replace_space_with=None):
    """
    Remove invalid file name chars from the specified name

    :param name: the file name
    :param replace_space_with: if not none replace space with this string
    :return: a valid name for Win/Mac/Linux
    """

    # ref: https://en.wikipedia.org/wiki/Filename
    # ref: https://stackoverflow.com/questions/4814040/allowed-characters-in-filename
    # No control chars, no: /, \, ?, %, *, :, |, ", <, >

    # remove control chars
    name = ''.join(ch for ch in name if unicodedata.category(ch)[0] != 'C')

    cleaned_name = re.sub(r'[/\\?%*:|"<>]', '', name)
    if replace_space_with is not None:
        return cleaned_name.replace(' ', replace_space_with)
    return cleaned_name

在Windows操作系统上创建一个文件,并在文件名中给它一个像\这样的无效字符。结果,你会得到一个弹出窗口,所有的无效字符在一个文件名。

好的,看看文件系统的比较如果你只关心主要玩家的文件系统:

窗口(FAT32, NTFS):任何Unicode NUL除外 , \, /, :, *, ?, ", <, >, |.此外,开头或结尾没有空格字符,结尾没有句号。 Mac(HFS, HFS+):除:或/之外的任何有效Unicode Linux(ext[2-4]):除了NUL和/以外的任何字节

所以任何字节除了NUL, \, /,:, *, ?, ", <, >, |,你不能有文件/文件夹调用。或. .当然,没有控制字符。

更准确地说,MacOS X(现在称为MacOS) / in the Finder被解释为:在Unix文件系统中。

这样做是为了向后兼容苹果从经典Mac操作系统。

在Finder中的文件名中使用/是合法的,在终端中查看相同的文件时,它将显示一个:。

它也可以用另一种方式工作:你不能在终端的文件名中使用/,但是a:是可以的,并且会在Finder中显示为/。

有些应用程序可能会更加严格,禁止使用这两个字符,以避免混淆,或者因为它们保留了以前经典Mac OS的逻辑,或者为了平台之间的名称兼容性。