文本文件保存增加了自动创建不存在的目录功能, 增加了识别有效tex文件功能
This commit is contained in:
parent
3656586161
commit
b8cb4832c4
|
|
@ -11,6 +11,9 @@ def ReadTextFile(filepath): #读取文本格式的文件
|
|||
return data #返回文本格式文件的内容
|
||||
|
||||
def SaveTextFile(data,filepath): #写入文本格式的文件
|
||||
pathname = os.path.dirname(filepath)
|
||||
if not os.path.exists(pathname):
|
||||
os.mkdir(pathname)
|
||||
with open(filepath,"w",encoding="u8") as f:
|
||||
f.write(data)
|
||||
return filepath #返回文件名
|
||||
|
|
@ -26,13 +29,15 @@ def load_dict(filename): #根据filename读取json数据库并转化为python字
|
|||
adict = json.loads(f.read())
|
||||
return adict #返回python字典
|
||||
|
||||
def save_dict(adict,filename): #将adict字典转化为json文件并保存至filename文件中
|
||||
def save_dict(adict,filepath): #将adict字典转化为json文件并保存至filename文件中
|
||||
try:
|
||||
with open(filename,"w",encoding = "u8") as f:
|
||||
f.write(json.dumps(adict,indent=4,ensure_ascii=False))
|
||||
return 0 #成功则返回0
|
||||
jsondata = json.dumps(adict,indent=4,ensure_ascii=False)
|
||||
SaveTextFile(jsondata,filepath)
|
||||
return filepath # 成功则返回文件路径
|
||||
except:
|
||||
return 1 #不成功则返回1
|
||||
print("保存 %s 文件失败"%filepath)
|
||||
return 1 # 失败则返回1
|
||||
|
||||
|
||||
|
||||
def pre_treating(string): #删除字符串中对比较无用的字符, 以供比较
|
||||
|
|
@ -777,5 +782,20 @@ def GenerateTeacherBodyString(problems,sectiontitles,prodict,objdict,consecutive
|
|||
bodystring += sectionstring
|
||||
return bodystring #返回主题内容字符串
|
||||
|
||||
def GetValidTexFiles(path): #获取有题号的.tex文件列表 及 有题号的.tex文件所在的路径列表
|
||||
texlist = []
|
||||
pathlist = []
|
||||
for root,folders,files in os.walk(path):
|
||||
for file in files:
|
||||
if file[-4:] == ".tex":
|
||||
texdata = ReadTextFile(os.path.join(root,file))
|
||||
ids = re.findall(r"\((\d{6})\)",texdata)
|
||||
if len(ids) > 0:
|
||||
pathlist = pathlist + [root]
|
||||
texlist = texlist + [os.path.join(root,file)]
|
||||
texlist = list(set(texlist))
|
||||
pathlist = list(set(pathlist))
|
||||
return (texlist,pathlist) # 返回 有题号的.tex文件列表 与 所在路径列表 组成的二元组
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("数据库工具, import用.")
|
||||
Reference in New Issue