to platform-specific directory tree listing shell commands such as dir / / on dos and windows since all files match "*"this just exhaustively generates all the file names in tree with single traversal because we can usually run such shell commands in python script with os popenthe following do the same workbut the first is inherently nonportable and must start up separate program along the wayimport os for line in os popen('dir / / ')print(lineend=''from pp tools find import find for name in find(pattern='*'startdir=')print(namewatch for this utility to show up in action later in this and bookincluding an arguably strong showing in the next section and cameo appearance in the grep dialog of ' pyedit text editor guiwhere it will serve central role in threaded external files search tool the standard library' find module may be gonebut it need not be forgotten in factyou must pass bytes pattern string for bytes filename to fnnmatch (or pass both as str)because the re pattern matching module it uses does not allow the string types of subject and pattern to be mixed this rule is inherited by our find find for directory and pattern see for more on re curiouslythe fnmatch module in python also converts bytes pattern string to and from unicode str in order to perform internal text processingusing the latin- encoding this suffices for many contextsbut may not be entirely sound for some encodings which do not map to latin- cleanly sys getfilesystemencoding might be better encoding choice in such contextsas this reflects the underlying file system' constraints (as we learned in sys getdefaultencoding reflects file contentnot namesin the absence of bytesos walk assumes filenames follow the platform' convention and does not ignore decoding errors triggered by os list dir in the "greputility of ' pyeditthis picture is further clouded by the fact that str pattern string from gui would have to be encoded to bytes using potentially inappropriate encoding for some files present see fnmatch py and os py in python' library and the python library manual for more details unicode can be very subtle affair cleaning up bytecode files the find module of the prior section isn' quite the general string searcher we're afterbut it' an important first step--it collects files that we can then search in an automated script in factthe act of collecting matching files in tree is enough by itself to support wide variety of day-to-day system tasks complete system programs