Python – IOError: [Errno 13] Permission denied:

I’m getting IOError: [Errno 13] Permission denied and I don’t know what is wrong wit this code.

I’m trying to read a file given an absolute path (meaning only file.asm),

and a relative path (meaning /.../file.asm), 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__':
    assem(sys.argv[1])


import sys

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

    table=SymbolTable()

    # max size of each word
    WORD_SIZE = 16
    # 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 assembler.py Users/***/Desktop/University/Add.asm

What am I doing wrong here?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just Close the opened file where you are going to write.

Method 2

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.

Method 3

You don’t have sufficient permissions to write to the root directory. See the leading slash on the filename?

Method 4

This happened to me when I was using ‘shutil.copyfile’ instead of ‘shutil.copy’. The permissions were messed up.

Method 5

I had a same problem. In my case, the user did not have write permission to the destination directory. Following command helped in my case :

chmod 777 University

Method 6

Maybe You are trying to open folder with open, check it once.

Method 7

For me nothing from above worked. So I solved my problem with this workaround. Just check that you have added SYSTEM in directory folder. I hope it will help somoene.

import os
# create file
@staticmethod
def create_file(path):
    if not os.path.exists(path):
        os.system('echo # > {}'.format(path))

# append lines to the file
split_text = text_file.split('n')
    for st in split_text:
        os.system('echo {} >> {}'.format(st,path))

Method 8

Check if you are implementing the code inside a could drive like box, dropbox etc. If you copy the files you are trying to implement to a local folder on your machine you should be able to get rid of the error.

Method 9

For me, this was a permissions issue.

Use the ‘Take Ownership’ application on that specific folder.
However, this sometimes seems to work only temporarily and is not a permanent solution.

Method 10

FYI I had this permission error because the file that it was trying to create was already open/used by another program (was created last time the script was run, I had opened it with excel, and then got a permission error when it was trying to recreate it)

leaving this here in case someone else finds it useful, it is not the real solution to the question asked

Method 11

I got this error because the directory didn’t exist yet.

Solution: create the directory

import os
if not os.path.exists(directory):
    os.makedirs(directory)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x