TypeError: forced to Unicode: need a string or buffer, type found

I am trying to write code that will read a file and do some manipulations on it.

the code:

def assem(file):
    import myParser
    from myParser import Parser
    import code
    import symboleTable
    from symboleTable import SymboleTable


newFile = "Prog.hack"
output = open(newFile, 'w')
input = open(file, 'r')


prsr=Parser(input)
while prsr.hasMoreCommands():
      str = "BLANK"
      if(parser.commandType() == Parser.C_COMMAND):
      str="111"+code.comp(prsr.comp())+code.dest(prsr.dest())+code.jump(prsr.jump())+"\n"

output.write(str)
prsr.advance()

the error i get is:

Traceback (most recent call last):
  File "assembler.py", line 11, in <module>
    input = open(file, 'r')
TypeError: coercing to Unicode: need string or buffer, type found

how do i run the program:

   python assembler.py Add.asm

where Add.asm id is the file I want to read, all modules are in the same library, including the .asm file.

+3
source share
2 answers

You have a lot of problems.

First, your indentation is inconsistent. This means that imports are considered part of the function assem, but nothing more. Literally the first thing you need to know about Python is that the indentation is significant.

Secondly, you use the built-in function name filefor your variable name. Do not do this.

-, assem. - . , input = open(file, 'r'), file , ( ).

, , import myParser, from myParser import Parser. .

+6

"C:\Python27\lib\ntpath.py", 488, abspath     path = _getfullpathname (path) TypeError: Unicode: , _function_or_method

-1

All Articles