Skip to content

Instantly share code, notes, and snippets.

@chrisyco
Created May 23, 2011 04:05
Show Gist options
  • Save chrisyco/986211 to your computer and use it in GitHub Desktop.
Save chrisyco/986211 to your computer and use it in GitHub Desktop.
Recursively rename UPPERCASE filename extensions to lowercase
#!/usr/bin/env python
"""Epic script to rename scary uppercase extensions (like .JPG) to
more friendly lowercase (like .jpg).
How to use
==========
Recursively rename all files in the current directory::
fixext
Recursively rename all files in another directory::
fixext /media/vulpix
Disable recursion with ``-d``::
fixext -d /media/ralts
Dry run (test it without actually renaming anything)::
fixext -n /media/vulpix
You can combine options, e.g.::
fixext -nd /media/ralts
That's it! Less is more, I say.
"""
from optparse import OptionParser
import os
import sys
def recursive_walk(path):
for root, dirs, files in os.walk(path):
for name in files:
yield os.path.join(root, name)
def simple_walk(path):
for name in os.listdir(path):
if os.path.isfile(name):
yield os.path.join(path, name)
def fix(name):
base, ext = os.path.splitext(name)
return base + ext.lower()
def main():
parser = OptionParser(usage='usage: %prog [options] [DIR...]')
parser.add_option('-d', '--no-recursion', action='store_false',
dest='recursion', default=True,
help="Don't recurse into subdirectories")
parser.add_option('-n', '--dry-run', action='store_true',
dest='dry_run', default=False,
help="Dry run: test it without actually renaming anything")
options, args = parser.parse_args()
if len(args) == 0:
paths = [os.getcwdu()]
else:
paths = args
if options.recursion:
walker = recursive_walk
else:
walker = simple_walk
dry_run = options.dry_run
n_renamed = 0
n_errors = 0
for path in paths:
for name in walker(path):
new_name = fix(name)
if name != new_name:
if os.path.exists(new_name):
print >>sys.stderr, "** Warning: destination '%s' already exists" % new_name
n_errors += 1
else:
print new_name
if not dry_run:
os.rename(name, new_name)
n_renamed += 1
print >>sys.stderr, "%d files renamed successfully, with %d errors" % (n_renamed, n_errors)
if n_errors > 0:
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment