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).
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.
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.
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.
File names cannot start or end with a period, nor can two consecutive periods appear.
The \\?\
syntax also works with cmd.exe
:
dir>"\\?\C:\whatever\test."
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With