如何使用 Python 在带有嵌入式斜杠的 Windows 上创建文件? [英] How can I create files on Windows with embedded slashes, using Python?

查看:67
本文介绍了如何使用 Python 在带有嵌入式斜杠的 Windows 上创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. The customer demands that file names have the following structure:

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

04/28/2012 04:07 PM 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.

有什么建议吗?

推荐答案

你不能.

正斜杠是Windows文件名中不允许使用的字符之一,见http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

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

以下基本规则使应用程序能够创建和处理文件和目录的有效名称,无论文件如何系统:

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.

根据需要使用反斜杠作为卷名的一部分,例如,C:\path\file"中的C:\"或\server\share"中的通用命名约定 (UNC) 的\server\share\path\file"名称.有关 UNC 名称的详细信息,请参阅最大路径长度限制部分.

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.

不要假设区分大小写.例如,考虑名称 OSCAR、Oscar 和 oscar 相同,即使某些文件系统(例如符合 POSIX 的文件系统)可能将它们视为不同的.请注意,NTFS 支持大小写的 POSIX 语义灵敏度,但这不是默认行为.更多信息,请参阅创建文件.

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.

卷指示符(驱动器号)同样不区分大小写.例如,D:\"和d:\"指的是同一个卷.

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

使用当前代码页中的任何字符作为名称,包括 Unicode 字符和扩展字符集 (128–255) 中的字符,但以下字符除外:

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)

整数值零,有时称为 ASCII NUL 字符.

Integer value zero, sometimes referred to as the ASCII NUL character.

整数表示在 1 到 31 范围内的字符,但允许这些字符的备用数据流除外.有关文件流的更多信息,请参阅文件流.

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 在带有嵌入式斜杠的 Windows 上创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆