'main filename = sys.argv[1] IndexError: list index out of range [duplicate]
I have a problem running the python program. When I run, it prints an error in the terminal :
main filename = sys.argv[1] IndexError: list index out of range
Would you please help me?
CODE:
def main():
filename = sys.argv[1]
rfile = open(filename, mode='r')
wfile = open(filename.split('.')[0]+'.hack',mode='w')
symbolTab = SymbolTable()
parser = Parser(rfile)
code = Code()
i = 0
while parser.hasMoreCommands():
ctype = parser.commandType()
if ctype == 'L_COMMAND':
symbol = parser.symbol()
symbolTab.addEntry(symbol,i-1)
else:
i += 1
parser.advance()
rfile.close()
Solution 1:[1]
Please change filename = sys.argv[1]
to filename = sys.argv[0]
For example, when you execute your python file like python3 test.py test
, sys.argv[1]
will be "test"
filename = os.path.basename(os.path.abspath(__file__))
just as effective
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CN-LanBao |