[python] 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?

This question is related to python file io

The answer is


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 Close the opened file where you are going to write.


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.


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

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

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


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


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


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to io

Reading file using relative path in python project How to write to a CSV line by line? Getting "java.nio.file.AccessDeniedException" when trying to write to a folder Exception: Unexpected end of ZLIB input stream How to get File Created Date and Modified Date Printing Mongo query output to a file while in the mongo shell Load data from txt with pandas Writing File to Temp Folder How to get resources directory path programmatically ValueError : I/O operation on closed file