为了账号安全,请及时绑定邮箱和手机立即绑定

FileNotFoundError: [Errno 2] 没有这样的文件或目录:

FileNotFoundError: [Errno 2] 没有这样的文件或目录:

慕桂英546537 2023-03-22 16:21:04
整个错误:C:\Users\Desktop\texts>python similarity1.pyTraceback (most recent call last):  File "similarity1.py", line 19, in <module>    documents = [open(f, encoding="utf-8").read() for f in text_files]  File "similarity1.py", line 19, in <listcomp>    documents = [open(f, encoding="utf-8").read() for f in text_files]FileNotFoundError: [Errno 2] No such file or directory: 'apempe_chunks.txt'以及产生特定错误的代码:import osimport codecsimport string, refrom pathlib import Pathpath = "C:\\Users\\Desktop\\texts\\dataset"text_files = os.listdir(path)documents = [open(f, encoding="utf-8").read() for f in text_files]sparse_matrix = tfidf_vectorizer.fit_transform(documents)奇怪的是程序找到了apempe_chunks.txt文件里面的那个dataset。我已经在 SO 中研究了这个问题,但我无法解决它。
查看完整描述

1 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

为了解决这个错误,我similarity1.pydataset文件夹中移动,将其添加到我的代码中if f.endswith('.txt')],现在它可以正常工作了。

所以现在完整的代码是

documents = [open(f, encoding="utf-8").read() for f in text_files if f.endswith('.txt')]

确保我只处理.txt数据集目录中的每个文件,不计算它自己的 python 脚本或其他文件。

这个想法来自这个答案线程,一个类似于我的问题。


查看完整回答
反对 回复 2023-03-22
  • 1 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号