windows 冒号/星号作为文件名分隔符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1665724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Colon/Asterisk as a filename delimiter?
提问by Smashery
I'm looking for a character to use a filename delimiter (I'm storing multiple filenames in a plaintext string). Windows seems not to allow :
, ?
, *
, <
, >
, "
, |
, /
and \
in filenames. Obviously, \
and /
can't be used, since they mean something within a path. Is there any reason why any of those others shouldn't be used? I'm just thinking that, similar to /
or \
, those other disallowed characters may have special meaning that I shouldn't assume won't be in path names. Of those other 7 characters, are any definitely safe or definitely unsafe to use for this purpose?
我正在寻找一个字符来使用文件名分隔符(我在一个纯文本字符串中存储多个文件名)。Windows 似乎不允许在文件名中使用:
, ?
, *
, <
, >
, "
, |
,/
和\
。显然,\
并且/
不能使用,因为它们意味着路径中的某些内容。有什么理由不应该使用其他任何一个吗?我只是在想,类似于/
or \
,那些其他不允许的字符可能具有特殊的含义,我不应该假设它们不会出现在路径名中。在其他 7 个字符中,用于此目的的字符是否绝对安全或绝对不安全?
回答by Guffa
The characters :
and "
are also used in paths. Colon is the drive unit delimiter, and quotation marks are used when spaces are part of a folder or file name.
字符:
和"
也用于路径。冒号是驱动单元分隔符,当空格是文件夹或文件名的一部分时使用引号。
The charactes *
and ?
are used as wildcards when searching for files.
搜索文件时,字符*
和?
用作通配符。
The characters <
and >
are used for redirecting an application's input and output to and from a file.
字符<
和>
用于将应用程序的输入和输出重定向到文件或从文件重定向。
The character |
is used for piping output from one application into input of another application.
该字符|
用于将一个应用程序的输出管道传输到另一个应用程序的输入。
I would choose the pipe character for separating file names. It's not used in paths, and its shape has a natural separation quality to it.
我会选择管道字符来分隔文件名。它不用于路径,它的形状具有自然的分离特性。
An alternative could be to use XML in the string. There is a bit of overhead and some characters need encoding, but the advantage is that it can handle any characters and the format is self explanatory and well defined.
另一种方法是在字符串中使用 XML。有一点开销,一些字符需要编码,但优点是它可以处理任何字符,并且格式不言自明且定义明确。
回答by Adrien Plisson
Windows uses the semicolon as a filename delimiter: ;
. look at the PATH environment variable, it is filled with ;
between path elements.
Windows 使用分号作为文件名分隔符:;
. 查看PATH环境变量,它;
在路径元素之间填充。
(Also, in Python, the os.path.pathsep
returns ";"
, while it expands to ":"
on Unix)
(此外,在 Python 中,os.path.pathsep
返回";"
,而它":"
在 Unix 上扩展到)
回答by MSalters
I have used *
in the past. The reason for portability to Linux/Unix. True, technically it can be used on those fileysystems too. In practice, all common OSes use it as a wildcard, thus it's quite uncommon in filenames. Also, people are not surprised if programs do break when you put a *
in a filename.
我过去用过*
。可移植到 Linux/Unix 的原因。确实,从技术上讲,它也可以用于那些文件系统。实际上,所有常见的操作系统都将它用作通配符,因此它在文件名中并不常见。此外,如果*
在文件名中放入 a 时程序确实中断,人们也不会感到惊讶。
回答by René K?bis
If all you need is the appearanceof a colon, and will be creating it programatically, why not make use of a UTF-8 character that just lookslike a colon?
如果您只需要冒号的外观,并且将以编程方式创建它,为什么不使用看起来像冒号的 UTF-8 字符呢?
My first choice would be the Modifier Letter (U+A789), as it is a typical RTL character and appears a lot like a colon. It is what I use when I need a full DateTime in the filename, such as file_2017-05-04_16?45?22_clientNo.jpg
我的第一个选择是修饰符字母 (U+A789),因为它是典型的 RTL 字符,看起来很像冒号。这是我在文件名中需要完整日期时间时使用的,例如file_2017-05-04_16?45?22_clientNo.jpg
I would stay away from characters like the Hebrew Punctuation Sof Pasuq (U+05C3), as it is a LTR character and may mess with how a system aligns the file name itself.
我会远离像 Hebrew Punctuation Sof Pasuq (U+05C3) 这样的字符,因为它是一个 LTR 字符并且可能会干扰系统如何对齐文件名本身。
回答by Greg Hewgill
It is actually possible to createfiles programmatically with every possible character except \
. (At least, this was true at one time and it's possible that Windows has changed its policy since.) Naturally, files containing certain characters will be harder to work with than others.
它实际上是可能的创建与除每一个可能的字符编程文件\
。(至少,这曾经是真的,而且 Windows 可能已经改变了它的策略。)自然,包含某些字符的文件将比其他文件更难处理。
What were you using to determine which characters Windows allows?
你用什么来确定 Windows 允许哪些字符?
Update: The set of characters allowed by Windows is also be determined by the underlying filesystem, and other factors. There is a blog entry on MSDNthat explains this in more detail.
回答by Ashish
Why dont you use any character with ALT key combination like ? (Alt + 0135) as delimiter ?
为什么不使用任何带有 ALT 组合键的字符?(Alt + 0135) 作为分隔符?