pythonioerror,IOError:[Errno 2]没有这样的文件或目录Python

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值