Python - IOError:[Errno 13]权限被拒绝: [英] Python - IOError: [Errno 13] Permission denied:

查看:241
本文介绍了Python - IOError:[Errno 13]权限被拒绝:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到 IOError:[Errno 13]权限被拒绝我不知道这段代码有什么问题。

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 ,这是文件名的第一个四个字符( /使用),附加 .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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆