Search code examples
pythonpython-3.xxmlwith-statement

Correct way to use with open on xml files and ET.parse


For reading in XML files with with open and ET.parse, should we use 'r', 'rb', something else, or does it not matter?

with open('something.xml', '??') as f:
    tree = ET.parse(f)

Edit:

The reason I want to open this way is because the filepath I have may be an s3 path and ET.parse does not appear to support S3 paths. However, using the package s3fs, I was able to load the file:

import s3fs
fs = s3fs.S3FileSystem()
with fs.open(file) as f:
    tree = ET.parse(f)

Solution

  • You dont need to open a file to pass it to ElementTree, it is smart enough to open for itself using the "rb" mode.

    As discussed here, It seems that the preferrable way, is to use "r", because the XML file is supposed to contain text encoded data. But, since the ElementTree uses "rb" mode, I dont think that there is a lot of difference.