How to create a filename with a trailing period in Windows?

血红的双手。 提交于 2019-11-30 14:04:44

问题


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).


回答1:


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.




回答2:


The \\?\ syntax also works with cmd.exe:

dir>"\\?\C:\whatever\test."



回答3:


Windows will strip the final trailing period, assuming it is the delimiter between a filename and a blank extension. Try using two periods.



来源:https://stackoverflow.com/questions/11681207/how-to-create-a-filename-with-a-trailing-period-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!