You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3中明明可见文件却触发FileNotFoundError的问题求助

Python 3中明明可见文件却触发FileNotFoundError的问题求助

我刚接触编程不久,最近在为一门数字人文课程写代码,想要通过循环处理一批文件,生成词干化后的文件列表来构建模型。我写的代码如下:

import nltk
from nltk.stem.porter import PorterStemmer

ps = PorterStemmer()
list_stemmed_files = []

for i in filenames:
    with open(str(i), 'r') as file:
        readFile = file.read()
        tokenized_file = nltk.tokenize.word_tokenize(readFile)
        stemmed_file = [ps.stem(word) for word in tokenized_file]
        list_stemmed_files.append(stemmed_file)

但运行这段代码时,我一直收到下面的错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-448-2c39e0a51e22> in <module>
      5 list_stemmed_files = []
      6 for i in filenames:
----> 7     with open (str(i),'r') as file:
      8         readFile = file.read()
      9         tokenized_file = nltk.tokenize.word_tokenize(readFile)

FileNotFoundError: [Errno 2] No such file or directory: 'K077983.000.txt'

我打印了filenames列表,能看到这些文件确实存在:

['K077983.000.txt',
'K096120.018.txt',
'K036380.000.txt',
'K039073.000.txt',
'K057408.000.txt',
'K040172.000.txt',
'K050373.000.txt',
'K041500.000.txt',
'K061112.000.txt',
'K119752.000.txt',
'K025532.001.txt',
'K020298.000.txt',
'K015852.000.txt',
'K010695.000.txt',
'K062887.000.txt']

请问我该怎么解决这个找不到文件的错误呢?

备注:内容来源于stack exchange,提问作者LesMisFan101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:05:29