Skip to content

Instantly share code, notes, and snippets.

@ndunn219
Last active June 14, 2016 13:54
Show Gist options
  • Save ndunn219/7a31e27b24f9e1a49297a7227896afd7 to your computer and use it in GitHub Desktop.
Save ndunn219/7a31e27b24f9e1a49297a7227896afd7 to your computer and use it in GitHub Desktop.
import os, re
dir = 'path-to-your-directory'
i=0
results = [] #Store changed files so you can look into it later
for dirname, dirnames, filenames in os.walk(dir):
for filename in filenames:
ext = filename.split('.')[-1] #Get Extension
if ext in ('cfc','cfm'): #Limit search to specific file types
i += 1
path = dirname + '/' + filename
with open(path) as f:
content = f.read()
#Get the line count before changes (for debugging)
l1 = len(content.splitlines())
if not re.search(r'(?<=\n)( *)\t', content):
print(i, 'NOT MODIFYING:', path)
continue
#Keep replacing leading tabs with two spaces until there are none
while re.search(r'(?<=\n)( *)\t', content):
content = re.sub(r'(?<=\n)( *)\t','\g<1> ', content)
#Get the line count before changes
l2 = len(content.splitlines())
#Write the changes back to the file
with open(path,'w') as f:
f.write(content)
results.append((i,path,l1,l2))
print(i, path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment