Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 322
Antimony Avatar asked Jul 27 '12 03:07

Antimony


People also ask

Can you use periods in file names Windows?

Illegal Filename CharactersDon't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.

Can you add periods to file names?

They are valid and you can use them but yes, there are disadvantages. A period is often used in regular expressions to represent a single character. A period in filenames is often used as the standard separator between filename and extensions.

Does Windows allow Colon in filename?

On Windows systems, files and directory names cannot be created with a colon (:). But if a file or directory name is created with a colon on a Linux or Mac operating system, then moved to a Windows system, percent encoding is used to include the colon in the name in the index.

Can a filename have two periods?

File names cannot start or end with a period, nor can two consecutive periods appear.


2 Answers

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

dir>"\\?\C:\whatever\test."
like image 32
Michel de Ruiter Avatar answered Sep 22 '22 00:09

Michel de Ruiter


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.

like image 62
Antimony Avatar answered Sep 21 '22 00:09

Antimony