0

我收到错误

line 9, in hasMoreCommands
    if self.asmfile.readline():
ValueError: I/O operation on closed file.

这是该文件的第 9 行:

class Parser:
  def __init__(self, asmfile):
    try:
      self.asmfile = open(asmfile,'r')
      self.currentCommand = ''  #initially there is no current command
    except FileNotFoundError:
      print("Wrong path")

  def hasMoreCommands(self):
    if self.asmfile.readline():
      return True
    return False         
  
  def advance(self):
    self.currentCommand = self.asmfile.readline()
    
  @property
  def commandType(self):
    self.currentCommand = self.currentCommand.strip()  #remove any leading or trailing whitespace
    
    #currentCommand is a comment or white line, not an instruction
    if self.currentCommand[0] == '/':
      return None
        
    elif not self.currentCommand.strip():
      return None            
    
    elif self.currentCommand[0] == '@':
      return 'A_COMMAND'
      
    else:
      return 'C_COMMAND'

  @property
  def symbol(self):
    if self.commandType == 'A_COMMAND':
      return self.currentCommand.strip('@')
    else:
        return None

  @property
  def dest(self):
    if self.commandType == 'C_COMMAND':
      if '=' in self.currentCommand:
        return self.currentCommand.split('=')[0]    
      else:
        return 'null'                               

  @property
  def comp(self):
    if self.commandType == 'C_COMMAND':
      if '=' in self.currentCommand and ';' in self.currentCommand:
        fields = self.currentCommand.split('=')   
        return fields[1].split(';')[0]            

      elif '=' in self.currentCommand:
        return self.currentCommand.split('=')[1]

      elif ';' in self.currentCommand:
        return self.currentCommand.split(';')[0]

  @property
  def jump(self):
    if self.commandType == 'C_COMMAND':
      if ';' not in self.currentCommand:
        return 'null'

      else:
        return self.currentCommand.split(';')[1]

主文件是这样的:

import sys
import ParserL as pl
import CodeL as cl

asmfilename = sys.argv[1]

hackfilename =  asmfilename.split('.')[0] + ".hack"
hackfile = open(hackfilename, "w")

# create a parser object, tied to the .asm file input
parser = pl.Parser(asmfilename)

# create a code object which will translate a symbol input to it
code = cl.Code()

while parser.hasMoreCommands():
    parser.advance()

    # if line read is blank or a comment
    if parser.commandType == None:
        continue

    elif parser.commandType == 'A_COMMAND':
        #parser.symbol is the decimal value to be converted in binary
        value = "{0:b}".format(parser.symbol)
        numZeroes = 16 - len(value)

        for i in range(numZeroes):
            hackfile.write("0")

        hackfile.write(value + "\n")

    elif parser.commandType == 'C_COMMAND':

        # symbolic values of the fields of the C command
        comp_symb = parser.comp
        dest_symb = parser.dest 
        jump_symb = parser.jump

        #binary representation of the fields of the C command
        comp_bin = code.comp(comp_symb)
        dest_bin = code.dest(dest_symb)
        jump_bin = code.jump(jump_symb)

        C_bin = '111' + comp_bin + dest_bin + jump_bin

        hackfile.write(C_bin + "\n")
        
    parser.asmfile.close()
    hackfile.close()

退出while循环后,我真的要关闭文件,所以我不确定为什么会收到此错误!

我没有使用with,因为我不确定如何使它适合代码。

另外,我为凌乱/丑陋的代码道歉,仍在弄清楚 Python!

4

0 回答 0