interfaces and set of ftp scripting techniquesdeleting all remote files just like the mirror scriptthe upload begins by asking whether we want to delete all the files in the remote target directory before copying any files there this cleanall option is useful if we've deleted files in the local copy of the directory in the client--the deleted files would remain on the server-side copy unless we delete all files there first to implement the remote cleanupthis script simply gets listing of all the files in the remote directory with the ftp nlst methodand deletes each in turn with the ftp delete method assuming we have delete permissionthe directory will be emptied (file permissions depend on the account we logged into when connecting to the serverwe've already moved to the target remote directory when deletions occurso no directory paths need to be prepended to filenames here note that nlst may raise an exception for some servers if the remote directory is emptywe don' catch the exception herebut you can simply not select cleaning if one fails for you we do catch deletion exceptionsbecause directory names like and may be returned in the listing by some servers storing all local files to apply the upload operation to each file in the local directorywe get list of local filenames with the standard os listdir calland take care to prepend the local source directory path to each filename with the os path join call recall that os listdir returns filenames without directory pathsand the source directory may not be the same as the script' execution directory if passed on the command line uploadingtext versus binary this script may also be run on both windows and unix-like clientsso we need to handle text files specially like the mirror downloadthis script picks text or binary transfer modes by using python' mimetypes module to guess file' type from its filename extensionhtml and text files are moved in ftp text modefor instance we already met the storbinary ftp object method used to upload files in binary mode--an exactbyte-for-byte copy appears at the remote site text-mode transfers work almost identicallythe storlines method accepts an ftp command string and local file (or file-likeobjectand simply copies each line read from the local file to same-named file on the remote machine noticethoughthat the local text input file must be opened in rb binary mode in python text input files are normally opened in text mode to perform unicode decoding and to convert any \ \ end-of-line sequences on windows to the \ platform-neutral character as lines are read howeverftplib in python requires that the text file be opened in rb binary modebecause it converts all end-lines to the \ \ sequence for transmissionto do soit must read lines as raw bytes with readlines and perform bytes string processingwhich implies binary mode files client-side scripting