Python - IOError: [Errno 13] Permission denied:

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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

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?

回答1:

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.



回答2:

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



回答3:

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



回答4:

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



回答5:

Of course you can. Try to change this line:

output = open(newFile, 'w') 

to:

output = open("./%s" % (newFile), 'w') 

it should save your file into local directory.



回答6:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!