title: Traversing FTP listing
body: <p>I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict</p>
  
  <p>So for example, a server that contains the following structure:</p>
  
  <pre><code>/www/
      mysite.com
          images
              png
              jpg
  </code></pre>
  
  <p>at the end of the script, would give me a list such as</p>
  
  <pre><code>['/www/'
    ['mysite.com'
      ['images'
        ['png'],
        ['jpg']
      ]
    ]
  ]
  </code></pre>
  
  <p>I have tried using a recursive function like so:
  def traverse(dir):
      FTP.dir(dir, traverse)</p>
  
  <p>FTP.dir returns lines in this format:</p>
  
  <pre><code>drwxr-xr-x    5 leavesc1 leavesc1     4096 Nov 29 20:52 mysite.com
  </code></pre>
  
  <p>so doing line[56:] will give me just the directory name(mysite.com). I use this in the recursive function.</p>
  
  <p>But i cannot get it to work. I've tried many different approaches and can't get it to work. Lots of FTP errors as well (either can't find the directory - which is a logical issue, and sometimes unexpected errors returned by the server, which leaves no log and i can't debug)</p>
  
  <p><strong>bottom line question:</strong>
  How to get a hierarchical directory listing from an FTP server?</p>