Skip to content

Instantly share code, notes, and snippets.

@snodnipper
Forked from jskeates/rename.py
Last active August 29, 2015 14:10
Show Gist options
  • Save snodnipper/28936561ac063cfccc06 to your computer and use it in GitHub Desktop.
Save snodnipper/28936561ac063cfccc06 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
import os, os.path, re
counterAll = 0
counterRenamed = 0
for root, _, files in os.walk("./data"):
for f in files:
counterAll += 1
fullpath = os.path.join(root, f)
replaced = re.sub("_OST50GRID\w*", "", f)
newfullpath = os.path.join(root, replaced)
if fullpath == newfullpath:
print 'Warning: not renaming ' + fullpath
else:
# print 'Renamed: "' + fullpath + '" to "' + newfullpath
counterRenamed += 1
os.rename(fullpath, newfullpath)
if counterAll == counterRenamed:
print 'Done. Renamed all ' + str(counterAll) + 'files'
else:
print 'Done. Renamed ' + str(counterRenamed) + ' of ' + str(counterAll) + ' files.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment