Created
January 8, 2018 11:48
-
-
Save conanca/815c6eadd2560fa5bce85269db061a65 to your computer and use it in GitHub Desktop.
修改当前目录下所有文件的MD5值以及文件名
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
from os.path import basename, isdir, splitext, dirname | |
from os import listdir, rename, urandom | |
import json | |
filename = {} | |
def traverse(path, depth=0): | |
print depth* '| ' + '|_', basename(path) | |
if(isdir(path)): | |
for item in listdir(path): | |
traverse(path+'/'+item, depth+1) | |
elif basename(path)!='modi.py': | |
modi(path,depth) | |
def modi(path, depth): | |
with open(path, 'ab+') as f: | |
print(depth* ' ' + " ++ begin to modify [%s]" % basename(path)) | |
f.write(b'EOF') | |
f.close() | |
print(depth* ' ' + ' ++ ok!') | |
random_name = ''.join(map(lambda xx:(hex(ord(xx))[2:]),urandom(16))) | |
new_path = dirname(path) +"/"+ random_name + splitext(path)[1] | |
rename(path,new_path) | |
filename[path] = new_path | |
print depth* ' ' + " ++ rename to :" + new_path | |
if __name__ == '__main__': | |
traverse('./') | |
jsObj = json.dumps(filename,ensure_ascii=False,indent=2) | |
with open('filename.json', 'w') as f: | |
f.write(jsObj) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment