Skip to content

Instantly share code, notes, and snippets.

@nicholastjohnson
Last active August 29, 2015 14:04
Show Gist options
  • Save nicholastjohnson/8f0be512821c8e646e60 to your computer and use it in GitHub Desktop.
Save nicholastjohnson/8f0be512821c8e646e60 to your computer and use it in GitHub Desktop.
Recursively remove or replace unwanted characters from filenames in a directory using Python
import os
paths = (os.path.join(root, filename)
for root, _, filenames in os.walk('C:\FolderName')
for filename in filenames)
for path in paths:
# the '#' in the example below will be replaced by the '-' in the filenames in the directory
newname = path.replace('#', '-')
if newname != path:
os.rename(path, newname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment