RDPD
RDPD

Reputation: 565

gensim file not found error

I am executing the following line:

id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')

This code is available at "https://radimrehurek.com/gensim/wiki.html". I downloaded the wikipedia corpus and generated the required files and wiki_en_wordids.txt is one of those files. This file is available in the following location:

~/gensim/results/wiki_en

So when i execute the code mentioned above I get the following error:

Traceback (most recent call last):
          File "~\Python\Python36-32\temp.py", line 5, in <module>
            id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')
          File "~\Python\Python36-32\lib\site-packages\gensim\corpora\dictionary.py", line 344, in load_from_text
            with utils.smart_open(fname) as f:
          File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 129, in smart_open
            return file_smart_open(parsed_uri.uri_path, mode)
          File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 613, in file_smart_open
            return open(fname, mode)
          FileNotFoundError: [Errno 2] No such file or directory: 'wiki_en_wordids.txt'

Even though the file is available in the required location I get that error. Should I place the file in any other location? How do I determine what the right location is?

Upvotes: 2

Views: 1555

Answers (1)

Prakhar Pratyush
Prakhar Pratyush

Reputation: 86

The code requires an absolute path here. Relative path should be used when entire operation is carried out in the same directory location, but in this case, the file name is passed as argument to some other function which is located at different location.

One way to handle this situation is using abspath -

    import os
    id2word = gensim.corpora.Dictionary.load_from_text(os.path.abspath('wiki_en_wordids.txt'))

Upvotes: 3

Related Questions