IOError:[Errno 2]没有这样的文件或目录Python [英] IOError: [Errno 2] No such file or directory Python

查看:2033
本文介绍了IOError:[Errno 2]没有这样的文件或目录Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,试图找到 *。vm 文件,并将它们发送到另一个

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.

这是主文件:

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()

应该读取行的模块:

#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)

错误是:

  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),

这里发生了什么?

推荐答案

os.listdir不包括路径,只有文件的名称。您可能希望以 os.path.join(fileName,child)作为参数调用Parser。

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.

这篇关于IOError:[Errno 2]没有这样的文件或目录Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆