Gensim ValueError:关闭文件的I / O操作

时间:2017-12-09 03:25:03

标签: python gensim

已解决:似乎引用我的toUse变量不能作为可接受的文件路径输入。将其更改为真正的路径可以解决问题。

我对Python比较新,而且我咬的比我咬得多,但我不明白我是如何在我的代码中得到这个错误的。它只是一个关闭文件的简单I / O操作但我不知道我的文件是如何关闭的文件,错误来自此代码块后面显示的corpora.MmCorpus.serialize()语句

from gensim import corpora
temporary=open('C:\\Users\\A\\Horror and Suspense\\data\\inUse.txt','r')
toUse=open('C:\\Users\\A\\Horror and Suspense\\data\\parsing.txt','r+')
for line in temporary:
    toUse.write(line)
temporary.close()
corpus=corpora.textcorpus.TextCorpus(input=toUse)
corpora.MmCorpus.serialize('C:\\Users\\A\\Horror and 
Suspense\\data\\corpora.mm',corpus)

这是错误:

runfile('C:/Users/A/Horror and Suspense/System1/useFiles.py',         wdir='C:/Users/A/Horror and Suspense/System1')
Traceback (most recent call last):

  File "<ipython-input-37-ccfa33041487>", line 1, in <module>
runfile('C:/Users/A/Horror and Suspense/System1/useFiles.py', wdir='C:/Users/A/Horror and Suspense/System1')

  File "C:\Users\A\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)

  File "C:\Users\A\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/A/Horror and Suspense/System1/useFiles.py", line 10, in <module>
corpora.MmCorpus.serialize('C:\\Users\\A\\Horror and Suspense\\data\\corpora.mm',corpus)

  File "C:\Users\A\Anaconda3\lib\site-packages\gensim\corpora\indexedcorpus.py", line 93, in serialize
offsets = serializer.save_corpus(fname, corpus, id2word, **kwargs)

  File "C:\Users\A\Anaconda3\lib\site-packages\gensim\corpora\mmcorpus.py", line 51, in save_corpus
fname, corpus, num_terms=num_terms, index=True, progress_cnt=progress_cnt, metadata=metadata

  File "C:\Users\A\Anaconda3\lib\site-packages\gensim\matutils.py", line 723, in write_corpus
    for docno, doc in enumerate(corpus):

  File "C:\Users\A\Anaconda3\lib\site-packages\gensim\corpora\textcorpus.py", line 184, in __iter__
    for text in self.get_texts():

  File "C:\Users\A\Anaconda3\lib\site-packages\gensim\corpora\textcorpus.py", line 250, in get_texts
for line in lines:

  File "C:\Users\A\Anaconda3\lib\site-packages\gensim\corpora\textcorpus.py", line 193, in getstream
with utils.file_or_filename(self.input) as f:

  File "C:\Users\A\Anaconda3\lib\site-packages\gensim\utils.py", line 148, in file_or_filename
input.seek(0)

ValueError: I/O operation on closed file.

0 个答案:

没有答案