2014-08-27 81 views
0

我正在使用python,并且正在运行使用同一目录中的文件的脚本,但它一直给我提示没有这样的文件。 顺便说一句,我使用该脚本文件作为模块来启用我的其他脚本文件。我使用的模块文件是从GitHub的项目:https://github.com/nik0spapp/unsupervised_sentiment(无监督的情感分析)为什么python在目录中有文件时说它没有目录中的文件?

Traceback (most recent call last): 
    File "sentiment_analysis.py", line 21, in <module> 
    import sentiment as unsupervised_sentiment 
    File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/sentiment.py", line 20, in <module> 
    from hp_classifiers import HpObj, HpSubj 
    File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/hp_classifiers.py", line 16, in <module> 
    from lexicon import Lexicon  
    File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/lexicon.py", line 17, in <module> 
    from datasets.emoticons_patch import patch_emoticons 
    File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/datasets/emoticons_patch.py", line 23, in <module> 
    emoticons_file = open("emoticons.data","r") 
IOError: [Errno 2] No such file or directory: 'emoticons.data' 

任何帮助将是最好的我到现在为止。谢谢!

+2

指定完整路径时会发生什么? – 2014-08-27 20:45:59

+0

你的意思是这样的? lib_path = os.path.abspath(“/ Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment /”)sys.path.append(lib_path) 导入情绪为unsupervised_sentiment – user2585578 2014-08-27 20:49:40

+1

我的意思只是' dir/dir/emoticons.data'显然,dir意味着你的任何目标。 – 2014-08-27 20:50:12

回答

0

在程序中使用相对路径而不是绝对路径时,您的代码正在执行程序的目录中寻找文件(如“emoticons.data”)(在本例中为脚本,而不是)首先尝试将所有文​​件路径更改为程序中的绝对路径,或将“emoticons.data”放入执行目录中。

0

您的错误(emoticons_file = open("emoticons.data","r"))与您引用的源代码(无监督情绪/数据集/ emoticons_patch.py​​:23是emoticons_file = open("datasets/emoticons.data","r"))之间存在差异。但基本问题是一样的。该模块尝试使用相对路径名打开该文件,该名称仅在从项目目录执行时才有效。

如果您更新了我在回购协议中找到的代码,则必须cd到/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/才能运行它。

对我来说,这是打破了,项目维护者应该修复它。

相关问题