为何我要得到关于pylab.legend的UnboundLocalError或者No Such File错误?

2024-10-02 06:21:38 发布

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

好的,我知道有很多关于UnboundLocalError的问题。我读过它们,并试图解决我的问题,但不要认为我的错误直接来自于全局/局部变量问题,就像其他问题一样。在

我实际的项目是另一个项目,但是使用我在过去的问题中引用的一些基本代码,我可以得到相同的错误。它也要短得多,所以我认为再次引用这段代码可能更容易些。这是对Bird、Klein和Loper的“用Python进行自然语言处理”中代码的修改。在

import os, re, csv, string, operator
import nltk
from nltk.corpus import PlaintextCorpusReader
dir = '/Users/me/Dropbox/testimony/hearings/'

corpus_root = dir
hearings = PlaintextCorpusReader(corpus_root, ".txt")

cfd = nltk.ConditionalFreqDist(
     (target, fileid[:3])
     for fileid in hearings.fileids()
     if os.path.getsize(fileid) > 0 #to avoid empty files
     for w in hearings.words(fileid)
     for target in ['reform']
     if w.lower().startswith(target))
cfd.plot()

编辑:包括更多控制台输出,以回答下面关于回溯完整性的问题。我打开了一个新的编辑器,运行上面的代码,没有做任何其他事情。在

以下是错误的回溯:

^{pr2}$

在我缺乏经验的眼中,最后一个调用看起来像是有关为图形创建图例的某些事情出了问题。但是向后看,文件也有问题(我想也是这样)。在

如果在本例中调用hearings.fileids(),则返回{}。在

然而。如果我在heards=code中将文件扩展名改为.*,如下所示

hearings = PlaintextCorpusReader(corpus_root, ".*")

再次运行所有程序,我现在得到以下错误:

OSError: [Errno 2] No such file or directory: '105Congress/105House/CHRG-105hhrg37099.txt'

编辑:包括更多控制台输出,以回答下面关于回溯完整性的问题。我打开了一个新的编辑器,运行上面的代码,没有做任何其他事情。在

以下是完整的回溯:

Welcome to Canopy's interactive data-analysis environment!
with pylab-backend set to: qt
Type '?' for more information.

In[1]: %run /Users/me/Dropbox/testimony/python_code/hearings_ingest.py
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    181             else:
    182                 filename = fname
--> 183             __builtin__.execfile(filename, *where)

/Users/me/Dropbox/testimony/python_code/hearings_ingest.py in <module>()
     14 cfd = nltk.ConditionalFreqDist(
     15      (target, fileid[:3])
---> 16      for fileid in hearings.fileids()
     17      if os.path.getsize(fileid) > 0 #to avoid empty files
     18      for w in hearings.words(fileid)

/Users/me/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/nltk/probability.pyc in __init__(self, cond_samples)
   1727         defaultdict.__init__(self, FreqDist)
   1728         if cond_samples:
-> 1729             for (cond, sample) in cond_samples:
   1730                 self[cond].inc(sample)
   1731 

/Users/me/Dropbox/testimony/python_code/hearings_ingest.py in <genexpr>((fileid,))
     15      (target, fileid[:3])
     16      for fileid in hearings.fileids()
---> 17      if os.path.getsize(fileid) > 0 #to avoid empty files
     18      for w in hearings.words(fileid)
     19      for target in ['reform']

/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/genericpath.pyc in getsize(filename)
     47 def getsize(filename):
     48     """Return the size of a file, reported by os.stat()."""
---> 49     return os.stat(filename).st_size
     50 
     51 

OSError: [Errno 2] No such file or directory: '105Congress/105House/CHRG-105hhrg37099.txt'

In[2]:

在本例中,当我调用hearings.fileids()时,我得到的是我试图处理的~30K个文件名的完整列表。的确,我可以打电话给你

hearings.words('105Congress/105House/CHRG-105hhrg37099.txt')

并获取文件的初始内容——错误所说的文件不存在。在

详细信息: Python 2.7 编辑器:我已经在多个编辑器中尝试过了,包括Canopy 1.1.0、emacs24.3(我的标准编辑器)、命令行等。 macosx10.9.1(也可以在Crunchbang linux11下运行,通过parallels)。在

我们非常感谢任何帮助。在


Tags: 代码intargetforifos错误users

热门问题