如何创建在Windows结尾句的文件名? [英] How to create a filename with a trailing period in Windows?

查看:192
本文介绍了如何创建在Windows结尾句的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人怎么工作,在Python中的句号结尾的文件名?根据MSDN的网站上,这样的文件名是有效的在Windows,但每当我试图创建一个在Python,它消除了最后一期。我甚至尝试创建具有os.open原始文件描述符,但它仍然删除的时间。

例如,这将创建一个简单地命名为文件测试

  os.open('测试',os.O_CREAT | os.O_WRONLY,0777)

编辑:这里是确切的报价


  

关于文件名和目录的空间和点。这些限制是
  在Windows外壳程序 - 在Windows或Windows NT。使用'庆典',可以
  创建带有空格的文件(或点),两者在开始和结束
  一个文件名。然后,您可以列出,并在Explorer中打开这些文件,并
  你可以在shell(CMD.EXE)名单他们,但你不一定会
  能够从shell(尤其是尾部的空格打开它们,并
  点)。



解决方案

我想出如何做到这一点。显然,通过一个正常的文件名,将直接从C.调用API赢时为了创造怪异的文件名,甚至剥离期间,你必须使用 \\\\?\\ preFIX(这也将禁用相对路径和斜线转换)。

 打开(\\\\\\\\。\\\\ C:?\\\\ \\\\的任何测试','W')

这是丑陋和不可移植,但它的作品。

How does one work with filenames that end in a period in Python? According to MSDN's site, such filenames are valid in Windows, but whenever I try to create one in Python, it removes the final period. I even tried creating a raw file descriptor with os.open, but it still removes the period.

For example, this will create a file simply named 'test'

os.open('test.', os.O_CREAT | os.O_WRONLY, 0777)

Edit: Here is the exact quote

About spaces and dots in filenames and directories. The limits are in the windows shell -- not in Windows or NT. Using 'bash', you can create files with spaces (or dots), both, at the beginning and end of a filename. You can then list and open those files in explorer, and you can 'list' them in the shell (cmd.exe), but you won't necessarily be able to open them from the shell (especially trailing spaces and dots).

解决方案

I figured out how to do this. Apparently, passing a normal filename will strip the period even when calling the Win API directly from C. In order to create the weird filenames, you must use the \\?\ prefix (this also disables relative paths and slash conversion).

open('\\\\?\\C:\\whatever\\test.','w')

It's ugly and nonportable, but it works.

这篇关于如何创建在Windows结尾句的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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