没有这样的文件或目录:'GoogleNews-vectors-negative300.bin'

No such file or directory: 'GoogleNews-vectors-negative300.bin'

我有这个代码:

import gensim
filename = 'GoogleNews-vectors-negative300.bin'
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)

这是我的文件夹组织: image of my folder tree that shows that the .bin file is in the same directory as the file calling it, the file being ai_functions

但遗憾的是,我不确定为什么会出现错误提示找不到它。顺便说一句,我检查过,我确定文件没有损坏。有什么想法吗?

完整追溯:

  File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/run.py", line 1, in <module>
    from serv import app
  File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/__init__.py", line 13, in <module>
    from serv import routes
  File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/routes.py", line 7, in <module>
    from serv.ai_functions import checkplagiarism
  File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/ai_functions.py", line 31, in <module>
    model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1629, in load_word2vec_format
    return _load_word2vec_format(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1955, in _load_word2vec_format
    with utils.open(fname, 'rb') as fin:
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 188, in open
    fobj = _shortcut_open(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 361, in _shortcut_open
    return _builtin_open(local_path, mode, buffering=buffering, **open_kwargs)
FileNotFoundError: [Errno 2] No such file or directory: 'GoogleNews-vectors-negative300.bin'

Python 进程将认为 'current working directory' 处于活动状态,因此将用作普通相对文件名 GoogleNews-vectors-negative300.bin 的预期位置,这取决于您启动 Flask 的方式。

您可以打印出目录以确保——在 How do you properly determine the current script directory? 中查看一些方法——但我怀疑它可能只是 /Users/Ile-Maurice/Desktop/Flask/flaskapp/ 目录。

如果是这样,您可以 relatively-reference 您的文件具有相对于上述目录的路径...

serv/GoogleNews-vectors-negative300.bin

...或者您可以使用完整的 'absolute' 路径...

/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/GoogleNews-vectors-negative300.bin

...或者您可以将该文件移至其父目录,以便它位于您的 Flask run.py.

旁边