[Python] 纯文本查看 复制代码import os
import shutil
def text_edit_vhd(filepath):
with open(filepath,'r+') as file_head:
content = file_head.read()
file_head.seek(0,0)
file_head.write("`protect begin \n" + content)
file_head.seek(0,2)
file_head.write("\n`protect end ")
def text_edit_v(filepath):
with open(filepath,'r+') as file_head:
content = file_head.read()
file_head.seek(0,0)
file_head.write("`protect \n"+ content)
file_head.seek(0,2)
file_head.write(" \n`endprotect ")
def add_text(srcpath): #寻找本文件夹及子文件夹下所以符合要求的文本进行处理
srcdirs = os.listdir(srcpath)
for dir in srcdirs:
if not os.path.isfile(os.path.join(srcpath, dir)): #判断dir属性,为文件或者文件夹
add_text(os.path.join(srcpath, dir))
else :
if '.vhd' == dir[-4: ]:
print("正在处理文件中,文件路径为:"+os.path.join(srcpath, dir))
text_edit_vhd(os.path.join(srcpath, dir))
print("文件处理完成!")
elif '.v' == dir[-2:] :
print("正在处理文件中,文件路径为:"+os.path.join(srcpath, dir))
text_edit_v(os.path.join(srcpath, dir))
print("文件处理完成!")
path = os.getcwd()
print('当前文件路径为:'+ path)
os.chdir('..')
root_path = os.getcwd()
handle_path = root_path + '\handle'
print('当前文件路径为:' + handle_path)
shutil.copytree(path,handle_path) #备份后进行数据处理
add_text(handle_path)
print("run done\n")