1
titles = [line.rstrip() for line in open('./nlp_class/all_book_titles.txt')]

# copy tokenizer from sentiment example
stopwords = set(w.rstrip() for w in open('./nlp_class/stopwords.txt'))

I tried to run the python file 'books.py' but it gave this error:

Traceback (most recent call last):
  File "books.py", line 14, in <module>
    titles = [line.rstrip() for line in open('./nlp_class/all_book_titles.txt')]
FileNotFoundError: [Errno 2] No such file or directory: './nlp_class/all_book_titles.txt'

The full directory is: C:\Python36\python_bible\nlp_class
The 'books.py' file location: C:\Python36\python_bible\books.py
The full code is here: https://jsfiddle.net/4rd2knbu/

Hữu Linh
  • 153
  • 3
  • 10

1 Answers1

0

I suspect the issue is that you're using the UNIX path separator / on a Windows system instead of using the Windows path separator \. You can write OS-independent code using os.path.join (which will automatically use the correct separator):

import os

titles_file = os.path.join("nlp_class", "all_book_titles.txt")
titles = [line.rstrip() for line in open(titles_file)]

# copy tokenizer from sentiment example
stopwords_file = os.path.join("nlp_class", "stopwords.txt")
stopwords = set(w.rstrip() for w in open(stopwords_file))
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121