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

标签 python windows

在谷歌搜索半小时后,我很惊讶我找不到任何方法在 Windows 上创建名称中包含斜线的文件。客户要求文件名具有以下结构:

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

到目前为止,我还没有找到任何方法来对斜杠进行编码,使它们成为文件名而不是路径的一部分。

有什么建议吗?

最佳答案

你不能。

正斜杠是 Windows 文件名中不允许使用的字符之一,请参阅 http://msdn.microsoft.com/en-us/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. For more information about UNC names, see the Maximum Path Length Limitation section.

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. Note that NTFS supports POSIX semantics for case sensitivity but this is not the default behavior. For more information, see CreateFile.

Volume designators (drive letters) are similarly case-insensitive. For example, "D:\" and "d:\" refer to the same volume.

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:

    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.

Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.

Any other character that the target file system does not allow.

关于python - 如何使用 Python 在 Windows 上创建带有嵌入式斜线的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10708334/

相关文章:

python - Pandas - 用最少听过的艺术家过滤用户

c++ - 如何使用非托管 C++ 向 Excel 文件添加新行?

C#.NET 从 Web 应用程序访问串行端口

windows - 部署可移植 HTML/JavaScript Windows 应用程序的良好框架是什么?

Python copy.deepcopy() 在没有引发警告、异常或错误的情况下失败

python - '>' 未识别为命令行参数?如何将其作为命令行参数传递?

python - 导入错误 : cannot import name 'Timestamp'

python - 如何在 sklearn 中编写自定义估算器并对其使用交叉验证?

C++ 获取 Process.GetCurrentProcess().SessionId

windows - 当 async_write() 操作永远不会结束并且涉及一个链时会发生什么?