简体   繁体   English

"Python - IOError:[Errno 13] 权限被拒绝:"

[英]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.我收到IOError: [Errno 13] Permission denied ,我不知道这段代码有什么问题。

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;和一个相对路径(意思是/.../file.asm ),我希望程序将文件写入给定的任何路径 - 如果它是绝对的,它应该将它写入当前目录; 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?我在这里做错了什么?

只需关闭要写入的打开文件即可。

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.请注意在第二行代码中使用-4而不是4 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.这解释了为什么您的程序试图创建/Use.hack ,它是文件名 ( /Use ) 的四个字符,并附加.hack

You don't have sufficient permissions to write to the root directory.您没有足够的权限写入根目录。 See the leading slash on the filename?看到文件名的前导斜杠了吗?

This happened to me when I was using 'shutil.copyfile' instead of 'shutil.copy'.当我使用 'shutil.copyfile' 而不是 'shutil.copy' 时,这发生在我身上。 The permissions were messed up.权限搞砸了。

也许您正试图打开的文件夹with open ,检查一次。

For me nothing from above worked.对我来说,上面没有任何效果。 So I solved my problem with this workaround.所以我用这个解决方法解决了我的问题。 Just check that you have added SYSTEM in directory folder.只需检查您是否在目录文件夹中添加了 SYSTEM。 I hope it will help somoene.我希望它会帮助 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))

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

检查您是否正在诸如 box、dropbox 等驱动器中实现代码。如果您将尝试实现的文件复制到您机器上的本地文件夹中,您应该能够摆脱错误。

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.然而,这有时似乎只是暂时的,并不是永久的解决方案。

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)仅供参考我有这个权限错误,因为它试图创建的文件已经被另一个程序打开/使用(上次运行脚本时创建,我用excel打开它,然后在尝试时遇到权限错误重新创建它)

leaving this here in case someone else finds it useful, it is not the real solution to the question asked留在这里以防其他人觉得它有用,这不是所问问题的真正解决方案

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM