简体   繁体   English

如何使用Python在带有嵌入式斜杠的Windows上创建文件?

[英]How can I create files on Windows with embedded slashes, using Python?

After a half hour searching Google, I am surprised I cannot find any way to create a file on Windows with slashes in the name. 在搜索Google半小时之后,我感到惊讶的是,我找不到在Windows上创建名称带有斜杠的文件的任何方法。 The customer demands that file names have the following structure: 客户要求文件名具有以下结构:

04/28/2012 04:07 PM 6,781 12Q1_C125_G_04-17.pdf 2012年4月28日04:07 6,781 12Q1_C125_G_04-17.pdf

So far I haven't found any way to encode the slashes so they become part of the file name instead of the path. 到目前为止,我还没有找到任何对斜杠进行编码的方法,因此它们成为文件名而不是路径的一部分。

Any Suggestions? 有什么建议么?

You can't. 你不能

The forward slash is one of the characters that are not allowed to be used in Windows file names, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx 正斜杠是Windows文件名中不允许使用的字符之一,请参阅http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa365247(v=vs.85)。 aspx

The following fundamental rules enable applications to create and process valid names for files and directories, regardless of the file system: 以下基本规则使应用程序可以创建和处理文件和目录的有效名称,而与文件系统无关:

Use a period to separate the base file name from the extension in the name of a directory or file. 使用句点将基本文件名与目录或文件名中的扩展名分开。

Use a backslash ( \\ ) to separate the components of a path. 使用反斜杠( \\ )分隔路径的各个组成部分。 The backslash divides the file name from the path to it, and one directory name from another directory name in a path. 反斜杠将文件名从路径分开,并将一个目录名与路径中的另一个目录名分开。 You cannot use a backslash in the name for the actual file or directory because it is a reserved character that separates the names into components. 您不能在实际文件或目录的名称中使用反斜杠,因为它是一个保留字符,将名称分隔为多个部分。

Use a backslash as required as part of volume names, for example, the "C:\\" in "C:\\path\\file" or the "\\server\\share" in "\\server\\share\\path\\file" for Universal Naming Convention (UNC) names. 作为卷名的一部分,请根据需要使用反斜杠,例如,对于Universal,“ C:\\ path \\ file”中的“ C:\\”或“ \\ server \\ share \\ path \\ file”中的“ \\ server \\ share”命名约定(UNC)名称。 For more information about UNC names, see the Maximum Path Length Limitation section. 有关UNC名称的更多信息,请参见最大路径长度限制部分。

Do not assume case sensitivity. 不要假设大小写敏感。 For example, consider the names OSCAR, Oscar, and oscar to be the same, even though some file systems (such as a POSIX-compliant file system) may consider them as different. 例如,即使某些文件系统(例如POSIX兼容文件系统)可能认为它们不同,也应将名称OSCAR,Oscar和oscar视为相同。 Note that NTFS supports POSIX semantics for case sensitivity but this is not the default behavior. 请注意,NTFS支持POSIX语义以区分大小写,但这不是默认行为。 For more information, see CreateFile. 有关更多信息,请参见CreateFile。

Volume designators (drive letters) are similarly case-insensitive. 卷号(驱动器号)类似地不区分大小写。 For example, "D:\\" and "d:\\" refer to the same volume. 例如,“ D:\\”和“ d:\\”指的是同一卷。

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following: 使用当前代码页中的任何字符作为名称,包括Unicode字符和扩展字符集(128–255)中的字符,但以下各项除外:

  The following reserved characters: < (less than) > (greater than) : (colon) " (double quote) / (forward slash) \\ (backslash) | (vertical bar or pipe) ? (question mark) * (asterisk) 

Integer value zero, sometimes referred to as the ASCII NUL character. 整数值零,有时也称为ASCII NUL字符。

Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. 整数表示形式在1到31之间的字符,但允许这些字符的备用数据流除外。 For more information about file streams, see File Streams. 有关文件流的更多信息,请参见文件流。

Any other character that the target file system does not allow. 目标文件系统不允许的任何其他字符。

At least all windows installation i've seen won't let you create files with slashes in them. 至少我见过的所有Windows安装都不允许您创建带有斜杠的文件。 Even if it were possible somehow, by doing deepshit magic, it will probably screw up almost all applications, including windows explorer. 即使有可能,通过做深切的魔术,它可能会破坏几乎所有应用程序,包括Windows资源管理器。

you could abuse windows' unicode capabilities, though. 但是,您可能会滥用Windows的unicode功能。

Creating a file with (this is not a forward slash, it is "division slash", see http://www.fileformat.info/info/unicode/char/2215/index.htm ) in it's name works just fine, for example. 创建一个文件这是不是一个斜杠,它是“师斜线”,见http://www.fileformat.info/info/unicode/char/2215/index.htm )在它的名字只是罚款,例如。

Um... forward slash is not a legal character in a Windows file name? 嗯... Windows文件名中的正斜杠不是合法字符吗?

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx http://msdn.microsoft.com/zh-CN/library/windows/desktop/aa365247(v=vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Windows 上使用 pathlib 输出带有正斜杠的路径? - How can I output paths with forward slashes with pathlib on Windows? 使用 Python 的 Windows 路径中的双斜杠问题 - Problem with double slashes in Windows Path using Python 如何使用 python 的 split() 从正斜杠 substring 拆分带有正斜杠的字符串? - How do I split a string with forward slashes in from the forward slashes substring using split() of python? 如何使用Python提取RTF中嵌入的HTML? - How can I extract HTML embedded in RTF using Python? 如何在Windows GUI应用程序中将嵌入式Python连接到控制台的I / O? - How can I connect embedded Python to the console's I/O in a Windows GUI app? 如何在Python中创建文件,读写文件? - How can I create files , read and write files in Python? 使用 Python 在 Windows 7 中创建快捷方式文件 - Create shortcut files in Windows 7 using Python 如何在Windows的回收站中删除文件? 蟒蛇 - How can I delete files from recycle bin in windows? python 如何使用Python在Windows注册表上验证和创建值? - How can I verify and create values on the Windows registry with Python? 如何在Python(Windows)中创建无边界应用程序? - How can I create a borderless application in Python (windows)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM