if used they must be passed by namenot position the user argument instead can be passed either wayif it is passed at all keyword-only arguments here prevent passed verbose or refetch values from being incorrectly matched against the user argument if the user value is actually omitted in call exception protocol the caller is expected to handle exceptionsthis function wraps downloads in try/finally statement to guarantee that the local output file is closedbut it lets exceptions propagate if used in gui or run from threadfor instanceexceptions may require special handling unknown in this file self-test if run standalonethis file downloads an image file again from my website as selftest (configure for your server and file as desired)but the function will normally be passed ftp filenamessite namesand directory names as well file mode as in earlier examplesthis script is careful to open the local output file in wb binary mode to suppress end-line mapping and conform to python ' unicode string model as we learned in it' not impossible that true binary datafiles may have bytes whose value is equal to \ line-feed characteropening in text mode instead would make these bytes automatically expand to \ \ two-byte sequence when written locally on windows this is only an issue when run on windowsmode doesn' change end-lines elsewhere as we also learned in thoughbinary mode is required to suppress the automatic unicode translations performed for text in python without binary modepython would attempt to encode fetched data when written per default or passed unicode encoding schemewhich might fail for some types of fetched text and would normally fail for truly binary data such as images and audio because retrbinary writes bytes strings in xwe really cannot open the output file in text mode anyhowor write will raise exceptions recall that in textmode files require str stringsand binary mode files expect bytes since retrbinary writes bytes and retrlines writes strthey implicitly require binary and text-mode output filesrespectively this constraint is irrespective of end-line or unicode issuesbut it effectively accomplishes those goals as well as we'll see in later examplestext-mode retrievals have additional encoding requirementsin factftplib will turn out to be good example of the impacts of python ' unicode string model on real-world code by always using binary mode in the script herewe sidestep the issue altogether directory model this function currently uses the same filename to identify both the remote file and the local file where the download should be stored as suchit should be run in the directory where you want the file to show upuse os chdir to move to directories if needed (we could instead assume filename is the local file' nameand transferring files with ftplib