decode_file()函数将指定路径文件配批量修改后缀名,然后存放到另一个指定位置;
recover_file()函数将修改过的文件名后缀批量还原;
import os
def decode_file():
src_path = input(">>>Please input path need to decode:")
tgt_path = input(">>>Please input path to release decode file:")
L = []
for root, dirs, files in os.walk(src_path):
for file in files:
L.append(os.path.join(root, file))
for srcfile in L:
tgtfile = tgt_path + srcfile[len(src_path) + 1:]
tgtfile = tgtfile.split(".")[0] + "." + tgtfile.split(".")[-1] + "decode"
try:
src_f = open(srcfile, "rb")
except IOError:
print("###源文件打开错误,程序退出!!!")
src_f.close()
return
if not os.path.exists(os.path.dirname(tgtfile)):
os.makedirs(os.path.dirname(tgtfile))
try:
tgt_f = open(tgtfile, "wb")
except IOError:
print("###目标文件生产错误,程序退出!!!")
tgt_f.close()
return
tgt_f.write(src_f.read())
src_f.close()
tgt_f.close()
def recover_file():
src_path = input(">>>Please input path need to recover:")
L = []
for root, dirs, files in os.walk(src_path):
for file in files:
L.append(os.path.join(root, file))
for srcfile in L:
os.rename(srcfile,srcfile.split(".")[0] + "." + srcfile.split(".")[-1].replace("decode",""))
if __name__ == "__main__":
while True:
func = input("---------------------------------------\nMenu\nChoose a function:\n1.Decode file\n2.Recover file\n3.Exit\n---------------------------------------\n>>>")
if func == "1":
decode_file()
elif func == "2":
recover_file()
elif func == "3":
break
else:
print(">>>Error,choose again!\n")