Skip to content

Instantly share code, notes, and snippets.

@vgmoose
Created August 13, 2014 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgmoose/362b58ea088a29ffabd6 to your computer and use it in GitHub Desktop.
Save vgmoose/362b58ea088a29ffabd6 to your computer and use it in GitHub Desktop.
renamer.py
import pickle, os
# get all dotfiles (which could potentially contain dumped data
dotfiles = [x for x in os.listdir('.') if x.startswith('.')]
objects = []
filenames = []
for filename in dotfiles:
try:
# try to de-pickle each dot file
with open(filename, 'rb') as file:
objects.append(pickle.load(file))
filenames.append(filename)
except:
pass
#print objects
# prompt for information
oldname = unicode(raw_input("Old username: ").lower())
newname = unicode(raw_input("New username: ").lower())
if oldname == newname:
print("Names must be different")
exit()
# replace it in every dotfile, and write it back out
for x in range(0, len(objects)):
object = objects[x]
filename = filenames[x]
if newname not in object:
object[newname] = 0
if oldname in object:
object[newname] += object[oldname]
del object[oldname]
with open(filename, 'wb') as file:
try:
pickle.dump(object, file)
print("Successfully replaced %s with %s in %s" % (oldname, newname, filename))
except:
print("File writing error")
else:
print("Found no instance of %s in %s" % (oldname, filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment