Python xml.etree.ElementTree目录访问 [英] Python xml.etree.ElementTree directory acess

查看:203
本文介绍了Python xml.etree.ElementTree目录访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import xml.etree.ElementTree as ET

ID="000296166"
tree = ET.parse("\folder" + ID +'.xml')
root = tree.getroot()

我想做的是使用xml.etree.elementtree

它一直显示错误:

IOError: [Errno 22] invalid mode ('rb') or filename: '\x0colder000296166.xml'

我一直感觉自己在编写路径时做错了事...但是我无法在网上找到任何示例来了解它应该如何工作

I keep getting the feeling that I did something wrong when writing the path...but I can't find any examples online to see how it's supposed to work

推荐答案

\f被解释为页面制动器,并用十六进制代码0xC代替.您应该从路径中删除前导反斜杠.

\f is interpreted as the page brake and is replaced with hex code 0xC. You should remove leading backslash from path.

tree = ET.parse("folder" + ID +'.xml')

如果在字符串中使用反斜杠,则可以像这样\\

And if you use backslash inside strings it can be escaped like this \\

编辑

使用路径时,最好使用os.path模块:

When you work with paths it is better to use os.path module:

 import os 
 ...
 tree = ET.parse(os.path.join('folder', ID + '.xml'))

这篇关于Python xml.etree.ElementTree目录访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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