IOError: [Errno 2] No such file or directory Python

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!