(reorganizethe download script by wrapping its parts in functionsthey become reusable in other modulesincluding our upload program example - pp \internet\ftp\mirror\downloadflat_modular py #!/bin/env python ""#############################################################################use ftp to copy (downloadall files from remote site and directory to directory on the local machinethis version works the samebut has been refactored to wrap up its code in functions that can be reused by the uploaderand possibly other programs in the future else code redundancywhich may make the two diverge over timeand can double maintenance costs #############################################################################""import ossysftplib from getpass import getpass from mimetypes import guess_typeadd_type defaultsite 'home rmi netdefaultrdir defaultuser 'lutzdef configtransfer(site=defaultsiterdir=defaultrdiruser=defaultuser)""get upload or download parameters uses class due to the large number ""class cfpass cf nonpassive false passive ftp on by default in cf remotesite site transfer to/from this site cf remotedir rdir and this dir (means acct rootcf remoteuser user cf localdir (len(sys argv and sys argv[ ]or cf cleanall input('clean target directory first')[: in [' ',' 'cf remotepass getpass'password for % on % :(cf remoteusercf remotesite)return cf def istextkind(remotenametrace=true)""use mimetype to guess if filename means text or binary for ' htmlguess is ('text/html'none)text for ' jpegguess is ('image/jpeg'none)binary for ' txt gzguess is ('text/plain''gzip')binary for unknownsguess may be (nonenone)binary mimetype can also guess name from typesee pymailgui ""add_type('text/ -python-win'pyw'mimetypeencoding guess_type(remotenamestrict=falsemimetype mimetype or '?/?maintype mimetype split('/')[ if traceprint(maintypeencoding or ''not in tables allow extras type unknownget first part transferring directories with ftplib