Skip to content

Instantly share code, notes, and snippets.

@witoff
Last active December 14, 2015 12:39
Show Gist options
  • Save witoff/5088078 to your computer and use it in GitHub Desktop.
Save witoff/5088078 to your computer and use it in GitHub Desktop.
Recursively rename files to work nicely on NTFS.
import os
from os import path, listdir
bad = {'*':'_', '\\':'__'}
def getDirs(path):
return [ name for name in listdir(path) if path.isdir(path.join(thedir, name)) ]
def printAll(uri):
elements = listdir(uri)
#sanitize
for e in elements:
item = path.join(uri, e)
new_name = e
for c in bad:
new_name = new_name.replace(c, bad[c])
if new_name != e:
print 'renaming: %s to %s' % (item, new_name)
#os.rename(item, path.join(uri, new_name))
elements = listdir(uri)
# recurse
dirs = [d for d in elements if path.isdir(path.join(uri, d))]
for d in dirs: printAll(path.join(uri, d))
printAll(os.getcwd())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment