Python - IOError: [Errno 13] 权限被拒绝: [英] Python - IOError: [Errno 13] Permission denied:
问题描述
我收到 IOError: [Errno 13] Permission denied
我不知道这段代码有什么问题.
I'm getting IOError: [Errno 13] Permission denied
and I don't know what is wrong wit this code.
我正在尝试读取给定绝对路径的文件(仅表示 file.asm
),
I'm trying to read a file given an absolute path (meaning only file.asm
),
和一个相对路径(意思是 /.../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.
代码:
#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
给出的错误:
IOError: [Errno 13] Permission denied: '/Use.hack'
我执行代码的方式:
python assembler.py Users/***/Desktop/University/Add.asm
我在这里做错了什么?
推荐答案
您似乎正在尝试使用以下代码替换扩展名:
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"
注意在第二行代码中使用 -4
而不是 4
.这解释了为什么您的程序试图创建 /Use.hack
,它是您的文件名 (/Use
) 的前四个字符,附加 .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.
这篇关于Python - IOError: [Errno 13] 权限被拒绝:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!