这可能是重复,但无论如何直接与python中的导入有关。在python中导入模块时无法加载文件
我有一个目录结构类似如下:
Main/
sample.py
utils/preprocess.py , __init__.py
Data/stopwords.txt
在sample.py
from utils import preprocess
在preprocess.py
import codecs
stopwords_ = codecs.open('../Data/stopwords.txt' , encoding='utf-8')
stopwords_ = stopwords_.readlines()
现在,当我运行样本误差。 py IOError:[Errno 2]没有这样的文件或目录:'../Data/stopwords.txt'。我理解错误的症结所在,因为当我在preprocess.py中打印os.getcwd()时,我得到'/ home/username/Main'。
但是如何解决它。任何帮助,将不胜感激
在路径 – planet260
@ planet260中使用“Data/stopwords.txt” - 这不是一个整洁的方式,我猜。我正在寻找标准的解决方案。谢谢 。 –