FileNotFound error in python 3 even though I see the file

74 Views Asked by At

I'm still relatively new to coding and I am writing a loop so I can make a model with stemmed files for a digital humanities course. I inputted:


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)

but I keep getting:

---------------------------------------------------------------------------
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'

When I print my folder filenames I get

['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']

How do I fix this error?

1

There are 1 best solutions below

0
FbAR On

You might want to try adding the path to the directory that has the list of files you are looping through. You can import 'os' library for this and try the following:

import os
import nltk
from nltk.stem.porter import PorterStemmer
ps = PorterStemmer()
file_path = 'path/to/directory/' # you should use the path to your directory here

list_stemmed_files = []
for i in filenames:
    with open (os.path.join(file_path,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)