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

时间:2011-01-19 14:49:50

标签: python file-permissions

我已经构建了这段代码来专门识别.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

我遇到错误:

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'     

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

2 个答案:

答案 0 :(得分:9)

您需要更改行

tree = ET.parse(workspace)

tree = ET.parse(filename)

因为workspace是一个目录,而parse方法采用文件名。

答案 1 :(得分:0)

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

workspace = "D:\\J040083"

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

workspace = r"D:\J040083"