upload simple filesrecur into subdirectories ""localfiles os listdir(localdirfor localname in localfileslocalpath os path join(localdirlocalnameprint('uploading'localpath'to'localnameend='if not os path isdir(localpath)self uploadone(localnamelocalpathlocalnameself fcount + elsetryself connection mkd(localnameprint('directory created'exceptprint('directory not created'self connection cwd(localnamechange remote dir self uploaddir(localpathupload local subdir self connection cwd('change back up self dcount + print('directory exited'if __name__ ='__main__'ftp uploadall(ftp configtransfer(site='learning-python com'rdir='training'user='lutz'ftp run(transferact lambdaftp uploaddir(ftp localdir)print('done:'ftp fcount'files and'ftp dcount'directories uploaded 'like the flat upload scriptthis one can be run on any machine with python and sockets and upload to any machine running an ftp serveri run it both on my laptop pc and on other servers by telnet or ssh to upload sites to my isp the crux of the matter in this script is the os path isdir test near the topif this test detects directory in the current local directorywe create an identically named directory on the remote machine with connection mkd and descend into it with connection cwdand recur into the subdirectory on the local machine (we have to use recursive calls herebecause the shape and depth of the tree are arbitrarylike all ftp object methodsmkd and cwd methods issue ftp commands to the remote server when we exit local subdirectorywe run remote cwd('to climb to the remote parent directory and continuethe recursive call level' return restores the prior directory on the local machine the rest of the script is roughly the same as the original in the interest of spacei'll leave studying this variant in more depth as suggested exercise for more contexttry changing this script so as not to assume that the toplevel remote directory already exists as usual in softwarethere are variety of implementation and operation options here here is the sort of output displayed on the console when the upload-all script is runuploading site with multiple subdirectory levels which maintain with site builder tools it' similar to the flat upload (which you might expectgiven that it is reusing client-side scripting