简体   繁体   中英

Error: No such file or directory

I am trying to extract data from a XML file with python. I tried the following code.

from xml.etree.ElementTree import ElementTree
 tree = ElementTree()
tree.parse("data_v2.xml")

Error message:

IOError: [Errno 2] No such file or directory: 'data_v2.xml'.

This is not XML error. This means that data_v2.xml does not exist -- system (operation system) cannot find it. Maybe this name is wrong, maybe you need to provide full path.

import traceback
# ...
try:
    input_fname = "data_v2.xml"
    tree.parse(input_fname)
    # ...
except IOError:
    ex_info = traceback.format_exc()
    print('ERROR!!! Cannot parse file: %s' % (input_fname))
    print('ERROR!!! Check if this file exists and you have right to read it!')
    print('ERROR!!! Exception info:\n%s' % (ex_info))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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