ftp.py 531 B

123456789101112131415
  1. from ftplib import error_perm
  2. from posixpath import dirname
  3. def ftp_makedirs_cwd(ftp, path, first_call=True):
  4. """Set the current directory of the FTP connection given in the ``ftp``
  5. argument (as a ftplib.FTP object), creating all parent directories if they
  6. don't exist. The ftplib.FTP object must be already connected and logged in.
  7. """
  8. try:
  9. ftp.cwd(path)
  10. except error_perm:
  11. ftp_makedirs_cwd(ftp, dirname(path), False)
  12. ftp.mkd(path)
  13. if first_call:
  14. ftp.cwd(path)