From ee6eb45d4b122b33dca01f549a3a8d49aa25e7dc Mon Sep 17 00:00:00 2001 From: torch <178428409@qq.com> Date: Mon, 21 Nov 2022 22:38:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9FileOpen=E7=B1=BB=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86encoding=E7=9A=84=E9=80=89=E9=A1=B9=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=AF=B9=E6=89=80=E6=9C=89open=E5=87=BD=E6=95=B0=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E4=BA=86=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Yaml.py | 15 ++++++++++----- app/difficulty.py | 8 ++++++-- app/file_open.py | 10 +++++----- app/pickle_idea.py | 20 ++++++++++++++------ app/pickle_idea2.py | 17 ++++++++++++----- app/wordfreqCMD.py | 8 ++++++-- 6 files changed, 53 insertions(+), 25 deletions(-) diff --git a/app/Yaml.py b/app/Yaml.py index 00974aa..8823041 100644 --- a/app/Yaml.py +++ b/app/Yaml.py @@ -1,13 +1,15 @@ -''' +""" Yaml.py 配置文件包括: ./static/config.yml ./layout/partial/header.html ./layout/partial/footer.html -''' +""" import yaml as YAML import os +from app.file_open import FileOpen + path_prefix = './' # comment this line in deployment # YAML文件路径 @@ -15,13 +17,16 @@ ymlPath = path_prefix + 'static/config.yml' # partial文件夹路径 partialPath = path_prefix + 'layout/partial/' -f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件 +# f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件 +f = FileOpen.read_only(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件 cont = f.read() # 以文本形式读取YAML yml = YAML.load(cont, Loader=YAML.FullLoader) # 加载YAML -with open(partialPath + 'header.html', 'r', encoding='utf-8') as f: +# with open(partialPath + 'header.html', 'r', encoding='utf-8') as f: +with FileOpen.read_only(partialPath + 'header.html', 'r', encoding='utf-8') as f: yml['header'] = f.read() # header内的文本会被直接添加到所有页面的head标签内 -with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f: +# with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f: +with FileOpen.read_only(partialPath + 'footer.html', 'r', encoding='utf-8') as f: yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部 diff --git a/app/difficulty.py b/app/difficulty.py index 50aa179..5eb12bd 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -7,11 +7,14 @@ import pickle import math + +from app.file_open import FileOpen from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order def load_record(pickle_fname): - f = open(pickle_fname, 'rb') + # f = open(pickle_fname, 'rb') + f = FileOpen.read_only(pickle_fname, 'rb') d = pickle.load(f) f.close() return d @@ -237,7 +240,8 @@ We need — for our farmers, our manufacturers, for, frankly, unions and non-uni #f = open('bbc-fulltext/bbc/entertainment/001.txt') - f = open('wordlist.txt') + # f = open('wordlist.txt') + f = FileOpen.read_only('wordlist.txt') s = f.read() f.close() diff --git a/app/file_open.py b/app/file_open.py index ee5512a..69d2f9d 100644 --- a/app/file_open.py +++ b/app/file_open.py @@ -15,7 +15,7 @@ class FileOpen: pass @staticmethod - def read_only(path, mode='r'): + def read_only(path, mode='r', encoding=None): if mode not in ['r', 'r+', 'rb', 'rt', 'rb+', 'rt+']: raise ModeErrorException('the input mode is wrong') my_dir = os.path.dirname(path) # 获得路径的目录 @@ -23,17 +23,17 @@ class FileOpen: os.makedirs(my_dir) # 创建所有父文件夹 # 读文件需要判断文件是否存在,不存在则创建 if not os.path.exists(path): - open(path, mode) - return open(path, mode) + open(path, mode, encoding) + return open(path, mode, encoding) @staticmethod - def write_able(path, mode='w'): + def write_able(path, mode='w', encoding=None): if mode not in ['w', 'w+', 'wb', 'wt', 'wb+', 'wt+']: raise ModeErrorException('the input mode is wrong') my_dir = os.path.dirname(path) # 获得路径的目录 if not os.path.exists(my_dir): # 判断文件的所有父文件夹是否存在 os.makedirs(my_dir) # 创建所有父文件夹 - return open(path, mode) + return open(path, mode, encoding) if __name__ == "__main__": diff --git a/app/pickle_idea.py b/app/pickle_idea.py index 45bd19a..3c0c853 100644 --- a/app/pickle_idea.py +++ b/app/pickle_idea.py @@ -9,6 +9,8 @@ import pickle from datetime import datetime +from app.file_open import FileOpen + def lst2dict(lst, d): ''' @@ -37,14 +39,16 @@ def merge_frequency(lst1, lst2): def load_record(pickle_fname): - f = open(pickle_fname, 'rb') + # f = open(pickle_fname, 'rb') + f = FileOpen.read_only(pickle_fname, 'rb') d = pickle.load(f) f.close() return d def save_frequency_to_pickle(d, pickle_fname): - f = open(pickle_fname, 'wb') + # f = open(pickle_fname, 'wb') + f = FileOpen.write_able(pickle_fname, 'wb') #exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w'] exclusion_lst = [] d2 = {} @@ -55,20 +59,24 @@ def save_frequency_to_pickle(d, pickle_fname): f.close() def unfamiliar(path,word): - f = open(path,"rb") + # f = open(path,"rb") + f = FileOpen.read_only(path, 'rb') dic = pickle.load(f) dic[word] += [datetime.now().strftime('%Y%m%d%H%M')] - fp = open(path,"wb") + # fp = open(path,"wb") + fp = FileOpen.write_able(path, 'wb') pickle.dump(dic,fp) def familiar(path,word): - f = open(path,"rb") + # f = open(path,"rb") + f = FileOpen.read_only(path, 'rb') dic = pickle.load(f) if len(dic[word])>1: del dic[word][0] else: dic.pop(word) - fp = open(path,"wb") + # fp = open(path,"wb") + fp = FileOpen.write_able(path, 'wb') pickle.dump(dic,fp) if __name__ == '__main__': diff --git a/app/pickle_idea2.py b/app/pickle_idea2.py index 4055fc4..ab84901 100644 --- a/app/pickle_idea2.py +++ b/app/pickle_idea2.py @@ -11,6 +11,9 @@ import pickle from datetime import datetime +from app.file_open import FileOpen + + def lst2dict(lst, d): ''' Store the information in list lst to dictionary d. @@ -26,14 +29,16 @@ def lst2dict(lst, d): d[word] += dates def deleteRecord(path,word): - with open(path, 'rb') as f: + # with open(path, 'rb') as f: + with FileOpen.read_only(path, 'rb') as f: db = pickle.load(f) try: db.pop(word) except KeyError: print("sorry") - with open(path, 'wb') as ff: - pickle.dump(db, ff) + # with open(path, 'wb') as ff: + with FileOpen.write_able(path, 'wb') as ff: + pickle.dump(db, ff) def dict2lst(d): if len(d) > 0: @@ -56,14 +61,16 @@ def merge_frequency(lst1, lst2): def load_record(pickle_fname): - f = open(pickle_fname, 'rb') + # f = open(pickle_fname, 'rb') + f = FileOpen.read_only(pickle_fname, 'rb') d = pickle.load(f) f.close() return d def save_frequency_to_pickle(d, pickle_fname): - f = open(pickle_fname, 'wb') + # f = open(pickle_fname, 'wb') + f = FileOpen.write_able(pickle_fname, 'wb') exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w'] d2 = {} for k in d: diff --git a/app/wordfreqCMD.py b/app/wordfreqCMD.py index 9ee7e56..d421ef2 100644 --- a/app/wordfreqCMD.py +++ b/app/wordfreqCMD.py @@ -8,6 +8,8 @@ import string import operator import os, sys # 引入模块sys,因为我要用里面的sys.argv列表中的信息来读取命令行参数。 import pickle_idea +from app.file_open import FileOpen + def freq(fruit): ''' @@ -32,7 +34,8 @@ def youdao_link(s): # 有道链接 def file2str(fname):#文件转字符 - f = open(fname) #打开 + # f = open(fname) #打开 + f = FileOpen.read_only(fname) s = f.read() #读取 f.close() #关闭 return s @@ -80,7 +83,8 @@ def make_html_page(lst, fname): # word s += '
%d %s (%d)
' % (count, youdao_link(x[0]), x[0], x[1]) count += 1 - f = open(fname, 'w') + # f = open(fname, 'w') + f = FileOpen.write_able(fname) f.write(s) f.close()