0

I've never used from os import * So this duplicate suggestion is not valid. Please look at the code in the description.

I'm not sure why I am running into this error. I thought it would be a simple with open() method but it isn't working. I have a .txt file that I'm trying to open and I'm getting this error.

I'm not sure why it is asking for a integer vs a string to open a .txt file

import os
import spacy
import en_core_web_sm
import plac
from collections import Counter
from __future__ import unicode_literals

file = 'spacy_sample.txt'
with open(file, 'r' )as f:
    '''Do something'''


TypeError                                 Traceback (most recent call last)
<ipython-input-198-b6441a57e84e> in <module>()
----> 1 with open(file, 'r' )as f:
      2     print(f)

TypeError: an integer is required (got type str)
M4cJunk13
  • 419
  • 8
  • 22
  • 1
    You probably did `from os import open` or `from os import *` earlier in your code. The code you posted doesn't throw that error. – Aran-Fey Jan 31 '18 at 23:39
  • 1
    Possible duplicate of [An integer is required? open()](https://stackoverflow.com/questions/1046656/an-integer-is-required-open) – Aran-Fey Jan 31 '18 at 23:40
  • @Rawing 8 No, in fact, I forgot to import os in the very beginning. I'm doing this in a jupyter notebook so I've gone back in and `imported os`, then taken it out. – M4cJunk13 Jan 31 '18 at 23:42
  • Probably something, somewhere, is altering the reference to the builtin `open()` function. Try printing its value: `print(open)` – mhawke Jan 31 '18 at 23:44
  • @mhawke You forgot a '(' This is what i got using the `print((open))` `` – M4cJunk13 Jan 31 '18 at 23:46
  • @M4cJunk13 but this is iPython so it's possible to trample a built-in "permanently" which may also have happened. You may need to clear the namespace. – roganjosh Jan 31 '18 at 23:46
  • @M4cJunk13: actually I added an extra one, thanks. Anyway, it's not a good test because `print(os.open)` displays the same string. Is your script _exactly_ as you show in your question? – mhawke Jan 31 '18 at 23:47
  • @roganjosh By clearing the `namespace` do you mean deleting line `import os` and re-run? – M4cJunk13 Jan 31 '18 at 23:49
  • 1
    @M4cJunk13 specifically for Jupyter I don't know but I don't think your suggestion will work. For example, you'd do it in Spyder (or Canopy) [this way](https://stackoverflow.com/a/47738834/4799172). _But_, your latest comment was that `open` points to the built-in. Your error does not make sense with the code you have given so I'd suspect an iPython residual issue somewhere where `open` was redefined somehow, I just don't know where sorry. – roganjosh Jan 31 '18 at 23:51
  • 1
    @roganjosh I've managed to get it to work by deleting the entire notebook and creating a brand new one. I've never used `from os import *`, or did I even know about it. Perhaps something got cached into the notebook somehow. I'm not sure, but I guess this is a jupyter notebook issue. Thanks! – M4cJunk13 Jan 31 '18 at 23:56

0 Answers0