【问题标题】:Why does python say it does not have the file in the directory when there is the file in the directory?为什么目录中有文件时python说目录中没有文件?
【发布时间】:2014-08-27 20:44:28
【问题描述】:

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

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'

到目前为止,任何帮助对我来说都是最好的。谢谢!

【问题讨论】:

  • 指定完整路径时会发生什么?
  • 你的意思是这样吗? lib_path = os.path.abspath("/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/") sys.path.append(lib_path) 将情绪导入为 unsupervised_sentiment
  • 我的意思是 /dir/dir/emoticons.data 的完整路径显然 dir 意味着你的目录是什么。
  • 您能告诉我们您用来调用相关文件的代码吗?如果我们看不到您在做什么,就很难找出问题所在。
  • 我的意思是我可以看到 traceback 中发生了什么,但确切的上下文会有所帮助。

标签: python file directory ioerror


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    如果你更新到我在 repo 中找到的代码,你必须 cd 到 /Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/ 来运行它。

    对我来说,这是损坏的,项目维护人员应该修复它。

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2021-04-28
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      相关资源
      最近更新 更多