saraswathi
saraswathi

Reputation: 93

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'.

Upvotes: 2

Views: 5971

Answers (1)

Michał Niklas
Michał Niklas

Reputation: 54292

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))

Upvotes: 9

Related Questions