0

I am trying to parse XML, or better I am trying to test if I can access my XML file. However I got this error here:

 >>>> DomTree = xml.dom.minidom.parse("usc01")
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
DomTree = xml.dom.minidom.parse("usc01")
File "C:\Python34\lib\xml\dom\minidom.py", line 1960, in parse
return expatbuilder.parse(file)
File "C:\Python34\lib\xml\dom\expatbuilder.py", line 910, in parse
with open(file, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'usc01'

This is directly from the Python Shell, I tried to move the usc01.xml to the python34 folder, but still gives me this error.

Does anyone know how to fix this?

2
  • Did you try xml.dom.minidom.parse("usc01.xml") Commented Jun 26, 2015 at 21:04
  • yes, I tried usc01.xml before, but it gave me the same error Commented Jun 26, 2015 at 21:05

1 Answer 1

3

Try using an absolute path name, or changing to the directory where the file is located. E.g. either

>>> DomTree = xml.dom.minidom.parse("c:\\Users\\somebody\\Desktop\\usc01.xml")

or

>>> import os
>>> os.chdir("c:\\Users\\somebody\\Desktop")
>>> DomTree = xml.dom.minidom.parse("usc01.xml")

And, of course, include the .xml extension, if the file really has one. Microsoft thinks that the average user's brain cannot handle a file name and an extension at the same time but, for the file system, things are a bit more complicated than this... :-)


Update: The error in the comment below is related to the file's contents, which are presumably not valid unicode. It's hard to say what's going on, without knowing what the file contains in positions 2-3.

If the file's contents are in some encoding other than the default and translation is required, you should first open the file appropriately and then pass the file object to the parse method, e.g.:

>>> f = open("c:\\Users\\somebody\\Desktop\\usc01.xml", encoding="iso-8859-1")
>>> DomTree = xml.dom.minidom.parse(f)
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, so now I got this error message >>> DomTree = xml.dom.minidom.parse("C:\Users\Raffael\Documents\Professional Work\Watson Showcase\US Code XML Database\xml\usc01.xml") SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I just tried to open the file using f = open(...), I also opened the source code of the file and the encoding used is UTF-8. I am trying to see what is the character in positions 2-3, so I can try to fix it.
OK, did you try the above with encoding="utf-8"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.