Colon in Python file names

As we all know, Windows file names cannot contain colons. However, I had a problem that could be reproduced using the following code sample:

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 called file in D:\ , which is removable storage. It does not create anything in C:\ , which requires administrator rights to write; however, it creates a file in the current working directory.

I do not understand three things:

  • Why aren't any exceptions possible (with other forbidden characters, I get an IOError)?
  • Why is word1 file empty?
  • Why is the file created in the current working directory?
+6
source share
1 answer

Windows NTFS supports the file stream. You basically add data to a file, outside the file, and you cannot view it normally. When you created the file "word1: word2", the hidden stream "word2" is attached to "word1". If you copied the word1 file to another NTFS machine, the word2 data will come with you

Go here http://technet.microsoft.com/en-us/sysinternals/bb897440.aspx and download the thread program. Running it, you will see that word2 is the thread associated with word1

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

To really prove this, 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. Enter the following:

  c:\tmp> notepad word1.txt 

You will see an empty file. Now try the following:

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

You should see the text Testing streams .

+8
source

Source: https://habr.com/ru/post/975091/


All Articles