Python-FileNotFoundError:[Errno 2]没有这样的文件或目录
我试图运行python文件“books.py”,但它出现了以下错误:Python-FileNotFoundError:[Errno 2]没有这样的文件或目录,python,Python,我试图运行python文件“books.py”,但它出现了以下错误: 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')) 回溯(最近一次呼叫最后一次): 文件“books.
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'))
回溯(最近一次呼叫最后一次):
文件“books.py”,第14行,在
titles=[line.rstrip()用于打开的行('./nlp_class/all_book_titles.txt')]
FileNotFoundError:[Errno 2]没有这样的文件或目录:'./nlp\u class/all\u book\u titles.txt'
完整目录是:C:\Python36\python\u bible\nlp\u class“books.py”文件位置:C:\Python36\python\u bible\books.py
完整代码如下:我怀疑问题在于您在Windows系统上使用的是UNIX路径分隔符
/
,而不是Windows路径分隔符\
。您可以使用OS.path.join
(将自动使用正确的分隔符)编写与操作系统无关的代码:
我怀疑问题在于您在Windows系统上使用的是UNIX路径分隔符
/
,而不是Windows路径分隔符\
。您可以使用OS.path.join
(将自动使用正确的分隔符)编写与操作系统无关的代码:
文件
C:\Python36\python\u bible\nlp\u class\all\u book\u titles.txt是否存在?是的,两个文件都存在您是否尝试过给出绝对路径而不是相对路径:C:/Python36/python\u bible/nlp\u class/all\u book\u titles.txtJsFiddle不运行python代码;)文件C:\Python36\python\u bible\nlp\u class\all\u book\u titles.txt是否存在?是的,两个文件都存在您是否尝试过给出绝对路径而不是相对路径:C:/Python36/python\u bible/nlp\u class/all\u book\u titles.txtJsFiddle不运行python代码;)
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'
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))