#script to blind
#python 2.7.8
import os
import random
from collections import OrderedDict

#helper function for confirmation
def confirm(question):
	while True:
		answer = raw_input(question)
		if answer not in ['y', 'Y', 'n', 'N']:
			print 'please enter y or n.'
			continue
		if answer == 'y' or answer == 'Y':
			return True
		if answer == "n" or answer == 'N':
			return False

#Variables
labels = []
blindtext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
blinds = []
blindmap = {}

print "This script will generate blinds (-A-, -B-, -C- ...) for files in this folder."

#Asks for labels
while True:
	response = raw_input("Please enter label (portion of filename to blind), type 'done' if finished: ")
	if response == "done":
		print "Finished adding labels."
		break
	else:
		labels.append(response)
		labels = list(OrderedDict.fromkeys(labels)) # clean up duplicates in labels
		print "Labels: ",
		print labels

notfound = [] # list of items not found, for removal later
for label in labels: # looks for each label in filename
	found = False
	for filename in os.listdir("."):
		if label in filename:
			print "Label '" + label + "' found!"
			found = True
			break
	if found == False:
		print "Label '" + label + "' not found."
		notfound.append(label)

for label in notfound: # remove those not found
	labels.remove(label)

print "Generating blinds for labels found: ",
print labels

for x in range(len(labels)): # initialize blinds as -A-, -B-, -C-, -D-, -E-, -F-...
	blinds.append("-" + blindtext[x] + "-")

random.shuffle(blinds) # shuffle blinds

for i in range(len(labels)): # generate dict of labels and blinds
	blindmap[labels[i]] = blinds[i]

f = open('blinds.txt','w') # write blinds to file
for key in blindmap:
    f.write(key + "," + blindmap[key] + "\n")
f.close()

print "Blinds written to 'blinds.txt'. Look after you're done!"

if confirm("Ready to change filenames. Do you wish to continue? Make sure you're working with a *copy* of your data. [y/n]: "):
	print "Blinding filenames..."
	for filename in os.listdir("."):
		newfilename = filename
		for key in blindmap:
			newfilename = filename.replace(key, blindmap[key])
		os.rename(filename, newfilename)
		print filename + " processed."
	print "All done! Happy analysis!"
else:
	print "Operation canceled. Quitting!"

@@ -2054,18 +2054,35 @@
 name
+.replace('%5C ', '_')
 %0A%09%09for 
-key
+x
  in 
@@ -2091,19 +2091,90 @@
 indmap:%0A
-%09%09%09
+                        if x in filename:%0A                                
 newfilen
@@ -2196,19 +2196,17 @@
 replace(
-key
+x
 , blindm
@@ -2208,19 +2208,17 @@
 lindmap%5B
-key
+x
 %5D)%0A%09%09os.
@@ -2280,16 +2280,36 @@
 essed.%22%0A
+%09%09print newfilename%0A
 %09print %22

fix on windows