print('= if not diffsprint('no diffs found 'elseprint('diffs found:'len(diffs)for diff in diffsprint('-'diffchanges diffs in-place walkreport diffs list at each directory in the treethis script simply runs the dirdiff tool to detect unique namesand then compares names in common by intersecting directory lists it uses recursive function calls to traverse the tree and visits subdirectories only after comparing all the files at each level so that the output is more coherent to read (the trace output for subdirectories appears after that for filesit is not intermixednotice the misses listadded in the third edition of this bookit' very unlikelybut not impossiblethat the same name might be file in one directory and subdirectory in the other also notice the blocksize variablemuch like the tree copy script we saw earlierinstead of blindly reading entire files into memory all at oncewe limit each read to grab up to mb at timejust in case any files in the directories are too big to be loaded into available memory without this limiti ran into memoryerror exceptions on some machines with prior version of this script that read both files all at oncelike thisbytes open(path 'rb'read(bytes open(path 'rb'read(if bytes =bytes this code was simplerbut is less practical for very large files that can' fit into your available memory space (consider cd and dvd image filesfor examplein the new version' loopthe file reads return what is left when there is less than mb present or remaining and return empty strings at end-of-file files match if all blocks read are the sameand they reach end-of-file at the same time we're also dealing in binary files and byte strings again to suppress unicode decoding and end-line translations for file contentbecause trees may contain arbitrary binary and text files the usual note about changing this to pass byte strings to os listdir on platforms where filenames may generate unicode decoding errors applies here as well ( pass dir encode()on some platformsyou may also want to detect and skip certain kinds of special files in order to be fully generalbut these were not in my treesso they are not in my script one minor change for the fourth edition of this bookos listdir results are now gathered just once per subdirectory and passed alongto avoid extra calls in dirdiff--not huge winbut every cycle counts on the pitifully underpowered netbook used when writing this edition comparing directory trees