60

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?

Jerzyk
  • 3,662
  • 23
  • 40
Itzik984
  • 15,968
  • 28
  • 69
  • 107
  • 3
    most probably you don'nt have prmissions to write to the root directory - and rightly so, it seems – Ingo May 13 '12 at 22:34

11 Answers11

44

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

Vicky
  • 5,098
  • 2
  • 33
  • 31
  • 16
    This answer makes no sense for the question… –  Jun 26 '17 at 23:12
  • 2
    This worked for me, what this person means is, if you have the folder you're trying to save into open, you need to close it – Gramatik Jul 19 '18 at 21:18
18

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • yes, you are right. but now i get : IOError: [Errno 2] No such file or directory: 'Users/itzikhanoch/Desktop/University/Add.hack' im using 'w' why should he find any file? it supposed to write it. – Itzik984 May 13 '12 at 23:15
  • 4
    Now it looks like you are passing `User/...` instead of `/User/...`. Note the leading slash, that's important. Without the leading slash, you are trying to open a file in the *subdirectory of the current directory* called `User`. – Greg Hewgill May 13 '12 at 23:19
12

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

James Youngman
  • 3,623
  • 2
  • 19
  • 21
7

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

Jason
  • 79
  • 1
  • 1
2

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
Sukhi
  • 13,261
  • 7
  • 36
  • 53
  • 1
    This isn't giving the user write permissions, but giving everyone on your system *all* permissions; read, write and execute. I would recommend looking at user groups and permissions instead of opening up your folders like this! – Nander Speerstra Feb 19 '20 at 03:52
  • 1
    Totally agreee. That’s the reason I mentioned “in my case” a couple times. It was the app on Ubuntu EC2 in AWS which needed permission 777 to a particular folder. Besides, the solution approach can be either taken starting from block-all or allow-all approach and then optimising; depending on the use case. – Sukhi Feb 19 '20 at 04:59
1

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

letmecheck
  • 1,183
  • 8
  • 17
1

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))
PythonMan
  • 787
  • 10
  • 18
1

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.

  • Just wondering how this would impact it? I have this error, and the file is on a mounted Google Drive (Google Drive File Stream). – Aedam May 26 '20 at 13:37
0

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.

swyveu
  • 21
0

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

elfe
  • 69
  • 7
0

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)
Andrew
  • 281
  • 3
  • 15