title: Creating year / month / day folder structure with Python in FTP
body: <p>I'm looking for the option to create a year / month / day folder structure with the Python ftplib module</p>
  
  <p>Connection:</p>
  
  <pre><code>ftp = ftplib.FTP(ftp_servidor, ftp_usuario, ftp_clave)
  </code></pre>
  
  <p>Loading variables:</p>
  
  <pre><code>ftp_raiz = 'TEST/'
  filename = '2019-10-01T00-00-00.txt'
  </code></pre>
  
  <p>Function:</p>
  
  <pre><code>def cdTree(ftp, filename=None, path=None):
  
      if filename is not None:
          date = datetime.datetime.strptime(filename, '%Y-%m-%dT%H-%M-%S.txt')
          path = ftp_raiz + date.strftime('%Y') + '/' + date.strftime('%m') + '/' + date.strftime('%d')
          print filename
      if path != "":
          try:
              ftp.cwd(path)
          except error_perm as e:
              print e, ", creating folder"
              print path
              cdTree(ftp, path="/".join(path.split("/")[:-1]))
              ftp.mkd(path)
              ftp.cwd(path)
  
  cdTree(ftp, filename, 'TEST')
  </code></pre>
  
  <p>Finally I do not create the folder structure, and it throws me the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>2018-10-18T00-00-00.txt
  550 Failed to change directory. , creating folder
  2018/10/18
  550 Failed to change directory. , creating folder
  2018/10
  Traceback (most recent call last):
    File "ftp2.py", line 34, in &lt;module&gt;
      cdTree(ftp, filename)
    File "ftp2.py", line 30, in cdTree
      cdTree(ftp, path="/".join(path.split("/")[:-1]) )
    File "ftp2.py", line 31, in cdTree
      ftp.mkd(path)
    File "C:\python27\lib\ftplib.py", line 589, in mkd
      resp = self.sendcmd('MKD ' + dirname)
    File "C:\python27\lib\ftplib.py", line 251, in sendcmd
      return self.getresp()
    File "C:\python27\lib\ftplib.py", line 226, in getresp
      raise error_perm, resp
  ftplib.error_perm: 550 Create directory operation failed.
  </code></pre>
  
  <p><strong>Note:</strong> perform the test of creating only one folder and it works!</p>