Python FileNotFoundError:[Errno 2]没有这样的文件或目录

2024-05-17 09:54:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我试着运行python文件'书籍.py“但它给出了一个错误:

^{pr2}$

完整目录是:C:\Python36\python\u bible\nlp_class
的书籍.py'文件位置:C:\Python36\python\u bible\书籍.py
完整的代码在这里:https://jsfiddle.net/4rd2knbu/


Tags: 文件inpytxtfornlplineopen
1条回答
网友
1楼 · 发布于 2024-05-17 09:54:26

我怀疑问题是您在Windows系统上使用UNIX路径分隔符/,而不是使用Windows路径分隔符\。您可以使用os.path.join(这将自动使用正确的分隔符)编写独立于操作系统的代码:

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

相关问题 更多 >