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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4736616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:16:18  来源:igfitidea点击:

IOError: [Errno 13] Permission denied:

pythonfile-permissions

提问by Alice Duff

I have built this code to specifically identify a load of .XML files and to extract co-ordinates from those files. Here is my code:

我构建了这段代码来专门识别 .XML 文件的负载并从这些文件中提取坐标。这是我的代码:

from xml.etree import ElementTree as ET
import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)

workspace = "D:/J040083"
gp.workspace = workspace

for root, dirs, filenames in os.walk(workspace): # returms root, dirs, and files
    for filename in filenames:
        filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
        filename_zero = filename_split[0]
        extension = str.upper(filename_split[1])

        try:
            first_2_letters = str.upper(filename_zero[0] + filename_zero[1])
        except:
            first_2_letters = "XX"

        if first_2_letters == "LI" and extension == ".XML":
            tree = ET.parse(workspace)
            print tree.find('//{http://www.opengis.net/gml}lowerCorner').text
            print tree.find('//{http://www.opengis.net/gml}upperCorner').text

I am having trouble with an error:

我遇到了错误:

Message File Name   Line    Position    
Traceback               
    <module>    D:\J040083\TXT_EXTRACTION.py    32      
    parse   C:\Python25\Lib\xml\etree\ElementTree.py    862     
    parse   C:\Python25\Lib\xml\etree\ElementTree.py    579     
IOError: [Errno 13] Permission denied: 'D:/J040083'     

I definitely do have access to this folder! I have also tried making new, empty folders and putting just one .xml file in there but i get the same error! Does anyone have any idea what has gone wrong?

我绝对可以访问这个文件夹!我也尝试过创建新的空文件夹并在其中只放入一个 .xml 文件,但我遇到了同样的错误!有谁知道出了什么问题?

采纳答案by Spaceghost

You need to change the line

你需要改变行

tree = ET.parse(workspace)

to

tree = ET.parse(filename)

because workspace is a directory and the parse method takes a filename.

因为工作区是一个目录,解析方法采用文件名。

回答by sth

Maybe you just need to write the file path with \instead of /:

也许您只需要使用\而不是编写文件路径/

workspace = "D:\J040083"

Or, without backslash escaping as a raw string:

或者,没有反斜杠作为原始字符串转义:

workspace = r"D:\J040083"