-
Notifications
You must be signed in to change notification settings - Fork 6
/
renamer.py
48 lines (37 loc) · 1.25 KB
/
renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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))