Skip to content

Instantly share code, notes, and snippets.

@aleksas
Created July 14, 2019 22:39
Show Gist options
  • Save aleksas/e8dcd2813505b93f50e618960c5d551d to your computer and use it in GitHub Desktop.
Save aleksas/e8dcd2813505b93f50e618960c5d551d to your computer and use it in GitHub Desktop.
Function to create directories recursively.
from ftplib import FTP, FTP_TLS, error_perm
def cwd(ftp, path, ret=False):
def mkd_cwd(ftp, path):
if path != '':
try:
ftp.cwd(path)
except error_perm:
mkd_cwd(ftp, '/'.join(path.split('/')[:-1]))
try:
ftp.mkd(path)
except error_perm:
pass
ftp.cwd(path)
except Exception as e:
print (e)
if not path.startswith('/'):
path = ftp.pwd() + path
mkd_cwd(ftp, path)
if ret:
ftp.cwd(cur_dir)
if __name__ == '__main__':
# SOURCE: https://dlptest.com/ftp-test/
args = {
'host': 'ftp.dlptest.com',
'user': '[email protected]',
'passwd': 'fLDScD4Ynth0p4OJ6bW6qCxjh'
}
use_tls = False
ftp = (FTP_TLS if use_tls else FTP)(**args)
cwd(ftp, 'c/test/rel/path')
cwd(ftp, '/c/test/abs/path/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment