Created
August 13, 2014 09:51
-
-
Save vgmoose/362b58ea088a29ffabd6 to your computer and use it in GitHub Desktop.
renamer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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