listedby default it lists the current server directory related ftp methoddirreturns the list of line strings produced by an ftp list commandits result is like typing dir command in an ftp sessionand its lines contain complete file informationunlike nlst if you need to know more about all the remote filesparse the result of dir method call (we'll see how in later examplenotice how we skip and current and parent directory indicators if present in remote directory listingsunlike os listdirsome (but not allservers include theseso we need to either skip these or catch the exceptions they may trigger (more on this later when we start using dirtooselecting transfer modes with mimetypes we discussed output file modes for ftp earlierbut now that we've started transferring texttooi can fill in the rest of this story to handle unicode encodings and to keep line-ends in sync with the machines that my web files live onthis script distinguishes between binary and text file transfers it uses the python mimetypes module to choose between text and binary transfer modes for each file we met mimetypes in near example - where we used it to play media files (see the examples and description there for an introductionheremime types is used to decide whether file is text or binary by guessing from its filename extension for instancehtml web pages and simple text files are transferred as text with automatic line-end mappingsand images and tar archives are transferred in raw binary mode downloadingtext versus binary for binary files data is pulled down with the retrbinary method we met earlierand stored in local file with binary open mode of wb this file open mode is required to allow for the bytes strings passed to the write method by retrbinarybut it also suppresses line-end byte mapping and unicode encodings in the process againtext mode requires encodable text in python xand this fails for binary data like images this script may also be run on windows or unix-like platformsand we don' want \ byte embedded in an image to get expanded to \ \ on windows we don' use chunk-size third argument for binary transfers herethough--it defaults to reasonable size if omitted for text filesthe script instead uses the retrlines methodpassing in function to be called for each line in the text file downloaded the text line handler function receives lines in str string formand mostly just writes the line to local text file but notice that the handler function created by the lambda here also adds \ lineend character to the end of the line it is passed python' retrlines method strips all line-feed characters from lines to sidestep platform differences by adding \nthe script ensures the proper line-end marker character sequence for the local platform on which this script runs when written to the file (\ or \ \nfor this auto-mapping of the \ in the script to workof coursewe must also open text output files in text modenot in wb--the mapping from \ to \ \ on transferring directories with ftplib