操作代码可以这么写:
import jieba
import shutil
path=input('输入文本目录路径以/结尾:')
f=open(path+'eng1.txt','r',encoding='utf-8')
txt=f.read()
lines = [x for x in txt.strip().split('\n') if x != '']
inp=input()
if inp=='1':
words=jieba.cut(txt)
words=[w for w in words if w.isalpha()]
ws={}
for w in words:
ws[w]=ws.get(w,0)+1
lst=sorted(ws.items(),key=lambda x:x[1],reverse=True)
print(lst[:10])
with open(path+'log.txt','w',encoding='utf-8') as fs:
for x in lst:
fs.write(x[0]+":"+str(x[1]))
fs.write('\n')
elif inp=='2':
shutil.copy('eng1.txt','./helpother/eng2.txt')
elif inp=='3':
print(len(lines))
elif inp=='4':
print(max([len(l) for l in lines]), min([len(l) for l in lines]))
elif inp=='5':
with open(path+'eng1.txt', 'a', encoding='utf-8') as fa:
fa.write('\nhello world')
print('hello world')
elif inp=='6':
f.close()
print('操作结束')
else:
print('无效字符')
如有帮助,请点击采纳。