Itzik984 Itzik984 - 1 year ago 142
Python Question

Python - IOError: [Errno 13] Permission denied:

Im getting

IOError: [Errno 13] Permission denied
, i dont know what im doing wrong.

Im trying to read a file given an absolute path (meaning only

and a relative path (meaning
) and i want the program to write the file

to whatever path is given - if it is absolute, it should write it to the current dir.

otherwise, to the path given.

the code:

#call to main function
if __name__ == '__main__':

import sys
def assem(myFile):
from myParser import Parser
import code
from symbolTable import SymbolTable


#max size of each word
#rom address to save to
rom_addrs = 0
#variable address to save to
var_addrs = 16

#new addition
if (myFile[-4:] == ".asm"):
newFile = myFile[:4]+".hack"

output = open(newFile, 'w') <==== ERROR

the error given:

IOError: [Errno 13] Permission denied: '/Use.hack'

the way i execute the code :

python Users/***/Desktop/University/Add.asm

what am i doing wrong over here?

Answer Source

It looks like you're trying to replace the extension with the following code:

if (myFile[-4:] == ".asm"):
    newFile = myFile[:4]+".hack"

However, you appear to have the array indexes mixed up. Try the following:

if (myFile[-4:] == ".asm"):
    newFile = myFile[:-4]+".hack"

Note the use of -4 instead of just 4 in the second line of code. This explains why your program is trying to create /Use.hack, which is the first four characters of your file name (/Use), with .hack appended to it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download