I have this piece of code, trying to find *.vm files, and send them to another
module i did, which supposed to read the lines.
this is the main file:
def VMTranslte(fileName):
print "FILEOVER ",fileName
from parser import Parser
from codeWriter import CodeWriter
if (fileName[-3:] == ".vm"):
outputFile = fileName[:-3]+".asm"
myWrite = CodeWriter(outputFile)
myWrite.setFileName(fileName)
myParser = Parser(fileName)
myWrite.setFileName(fileName);
translate(myParser,myWrite)
else:
if fileName[-1:] == "/": <===== CHECKS FOR DIRECTORY
mystr = fileName.split('/')[-2]
mystr = mystr.split('.')[0]+".asm"
outputFile = fileName+mystr
else:
outputFile = fileName+".asm"
myWrite = CodeWriter(outputFile)
for child in os.listdir(fileName):
if child.endswith('.vm'): <===== CHECK IF THERE IS *.vm FILE
print "CHILD: ",child <===== PRINTS THE FILE WANTED (MEANING FINDS IT)
myWrite.setFileName(child);
myParser = Parser(child) <===== CALLS THE READER MODULE DESCRIBED AT THE BOTTOM
translate(myParser,myWrite)
myWrite.close()
the module which supposed to read the lines:
#Constructor for Parser module.
def __init__(self,fileName):
import re
self.cmds = []
self.counter = 0
myFile = open(fileName, 'r') <=====ERROR OVER HERE
fLines = myFile.readlines()
for value in fLines :
lineStrip = value.strip()
if not (re.match("//",lineStrip) or len(lineStrip)==0):
self.cmds.append(lineStrip)
the error is:
File "/Users/***/Desktop/dProj7/parser.py", line 19, in __init__
myFile = open(fileName, 'r')
IOError: [Errno 2] No such file or directory: 'BasicTest.vm'
it is clear that the script finds the file, (he goes in the first loop),
what is going on over here?
解决方案
os.listdir does not include the path, only the name of the file. You probably want to call Parser with os.path.join(fileName, child) as the argument.