亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號