Daniel Chen
Daniel Chen

Reputation: 2023

Python "open" file got 'TypeError: function takes at least 2 arguments (1 given)'

I've had a piece of code which basically read a txt file from directory.

def img2vector(filename):
  returnVect=zeros((1,1024))
  fr=open(filename)
  for i in range(32):
    lineStr=fr.readline()
    for j in range(32):
      returnVect[0,32*i+j]=int(lineStr[j])
  return returnVect

When I run img2vector(PATHTOMYFILE) I've got the error message:

    fr=open(filename)
TypeError: function takes at least 2 arguments (1 given)

But, when I try: fr=open(PATHTOMYFILE) it's correct without any problem.

Upvotes: 2

Views: 2440

Answers (2)

Don Wayne
Don Wayne

Reputation: 11

You must have imported both os and numpy

You can do

from numpy import *
import operator
import os

Upvotes: 0

NPE
NPE

Reputation: 500367

The most likely cause is that you define (or import) a function called open() that shadows the built-in.

Upvotes: 3

Related Questions