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
相关产品推荐
相关产品推荐

