I have an XML file named junit.xml in the same directory as my script, I can parse it by doing:
xml_file = os.path.abspath(__file__)
xml_file = os.path.dirname(xml_file)
xml_file = os.path.join(xml_file, "junit.xml")
root = ET.parse(xml_file).getroot(); # Where ET is the element tree
and everything is ok.
However, I have a more complex example where I need to parse a bunch files with the same name “junit.xml” that are in different directories in succession.
The directories are as follows:
\myhome\ireland\modules\builds\date1
\myhome\ireland\modules\builds\date2
\myhome\england\modules\builds\date1
\myhome\england\modules\builds\date2
\myhome\scotland\modules\builds\date1
\myhome\scotland\modules\builds\date2
\myhome\wales\modules\builds\date1
\myhome\wales\modules\builds\date2
\myhome\germany\modules\builds\date1
\myhome\germany\modules\builds\date2
Now, each directories has collection of XML files. I just want to get all the files named junit.xml under:
\myhome\ireland\modules\builds\date2
\myhome\england\modules\builds\date2
\myhome\scotland\modules\builds\date2
How can I do this in a pythonic way, where I can vary the names of the countries and the date when I need to?
Use a string template for the path, for example:
Which you can later use to construct the real path using the
str.format()function (e.g.path.format("ireland", 1)).Then, you can iterate over the country names and dates, and for each one parse the XML file:
Where
parse_xmlis a function you define which gets a path to an XML file and parses it.