问题描述
jieba 在 pyinstaller -F app.py打包之后换其他电脑无法正常使用,经定位后发现是jeiba需要缓存temp目录存在jieba.cache文件
解决方案
参考文章 Pyinstaller打包用spec添加资源文件,亲测可用
spec写入资源jieba.cache
binaries=[("jieba.cache",".")]
通过move移动资源【获取到当前脚本运行的位置 然后jieba在里面 直接拼接绝对位置然后移动到指定位置】
shutil.move(os.path.abspath(os.path.dirname(sys.path[0]))+"\\jieba.cache", os.getenv("TEMP")+"\\jieba.cache")
# 词频统计.py
import os
import sys
import jieba
import shutil
shutil.move(os.path.abspath(os.path.dirname(sys.path[0]))+"\\jieba.cache", os.getenv("TEMP")+"\\jieba.cache")
txt = open("词频.txt", "r",
encoding='utf8', errors='ignore').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
counts[word] = counts.get(word, 0) + 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
file = open(r'result.txt', 'w')
for i in range(len(items)):
word, count = items[i]
if count > 3 and not("-" in word):
result = "{0:<10}{1:>5}".format(word, count)
file.write(result+'\n')
print(result)
file.close()