title: Open files referenced in nested file structure
body: <p>I have an xml file structure with a main xml referencing some other xml files (with full path), and these others reference other ones, and so on.
  I have many xml files (in a simple folder structure, just a main folder and 3 subfolders) but not all of them are referenced within other xml files, so the idea is to build a list with ONLY the referenced xml files.</p>
  
  <p><a href="https://i.stack.imgur.com/6jvep.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6jvep.png" alt="enter image description here"></a></p>
  
  <p>[EDIT]
  This is a very simplified example of an xml file. main.xml is very much like this. Other files don't have the <em>children</em> tag so they are the end of the nested reference line.</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8" ?&gt;
   &lt;file&gt;
     &lt;indiv&gt;
          &lt;name&gt;Name Surname&lt;/name&gt;
          &lt;birth&gt; Oct 1826 &lt;/birth&gt;
          &lt;death&gt; Jan 1850 &lt;/death&gt;        
     &lt;/indiv&gt;
     &lt;children&gt;
          &lt;chname1&gt;Name1 Surname1&lt;/chname1&gt;
          &lt;chcode1&gt;F45DH3&lt;/chcode1&gt;&lt;chdata1&gt;C:\base\codedb\F45DH3.xml&lt;/chdata1&gt;
          &lt;chname2&gt;Name2 Surname2&lt;/chname2&gt;
          &lt;chdata2&gt;C:\base\namedb\name2sur2.xml&lt;/chdata2&gt;
     &lt;/children&gt;
   &lt;/file&gt;
  </code></pre>
  
  <p>[/EDIT]</p>
  
  <p>As there are too many files to check manually I want to use a python script to read all the files starting from my main path and build the list.
  How can I do it.</p>
  
  <p>This is what I have so far, but evidently these would be very long to code as I don't know how many levels of referencing I will find.
  Any idea?</p>
  
  <pre><code>coreFile= r"C:\\base\\main.xml"
  xmlList = []
  
  with open(coreFile) as f:
      for line in f:
          if "C:\\base" in line:
              start = line.find('C:\\base')
              end = line.find('.xml')
              path = line[start:end + 4]
  
              if path not in xmlList:
                  x.append(path)
                  with open(path) as f2:
                      for line2 in f2:
                          if "C:\\base" in line2:
                              start = line.find('C:\\base')
                              end = line.find('.xml')
                              path = line[start:end + 4]
  
                              if path not in xmlList:
                                  x.append(path)
                                  with open(path) as f3:
                                      # ...
  </code></pre>