Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colon in file names in Python

As we all know, filenames in Windows can't contain colons. However, I ran into a problem, that can be reproduced with the following sample code:

import os
os.chdir('./temp')
names = ['a', 'b', 'word1: word2', 'c: file', 'd: file']

for name in names:
    with open(name, 'w') as f:
        f.write('foo')

This script creates three files in the ./temp directory: a, b (with 'foo') and word1 (empty). It also creates a file named file in D:\, which is removable storage. It doesn't create anything in C:\, which requires administrator rights to write in; however, it does create a file in the current working directory.

I don't understand three things:

  1. Why aren't any exceptions thrown (with other forbidden characters, I get IOError)?
  2. Why is the word1 file empty?
  3. Why a file is created in the current working directory?
like image 560
Kriattiffer Avatar asked Sep 10 '14 20:09

Kriattiffer


People also ask

Can I use a colon in a file name?

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.

Why can't you use a colon in a filename?

A colon is an invalid character for a Windows file name. You won't be able to allow ':' in the file name, but you can work around it.

What can I use instead of a colon in a filename?

Unfortunately, the Mac's HFS+ file system uses the COLON as a path separator just as Unix-like OSes use SOLIDUS / (slash) and Microsoft OSes use REVERSE SOLIDUS \ (backslash). All three characters must be avoided when naming a file or folder for compatibility.

Is semicolon allowed in filename?

Semicolons are legal in NTFS file paths. 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)


1 Answers

Windows NTFS supports file "stream". You basically append data to a file, outside of the file, and can't be viewed normally. When you created the file "word1:word2", the hidden stream "word2" is attached to "word1". If you copied the file word1 to another NTFS machine, the word2 data would come with you

Go here http://technet.microsoft.com/en-us/sysinternals/bb897440.aspx and download the streams program. Running it will show you that word2 is a stream attached to word1

This page also talks about streams: http://www.forensicfocus.com/dissecting-ntfs-hidden-streams

To really prove this easily, you can use Notepad but you need to use the .txt extension:

 file=open('word1.txt:word2.txt','w')
 file.write('Testing streams')
 file.close()

Now, using the cmd program, change directories to where you created the files. Type the following:

 c:\tmp> notepad word1.txt

You will see an empty file. Now, try this:

 c:\tmp> notepad word1.txt:word2.txt

You should see the text Testing streams.

like image 182
cybergoof Avatar answered Sep 17 '22 19:09

cybergoof