print('copied'fcount'files,'dcount'directories'end='print('in'time clock(start'seconds'this script implements its own recursive tree traversal logic and keeps track of both the "fromand "todirectory paths as it goes at every levelit copies over simple filescreates directories in the "topathand recurs into subdirectories with "fromand "topaths extended by one level there are other ways to code this task ( we might change the working directory along the way with os chdir calls or there is probably an os walk solution which replaces from and to path prefixes as it walks)but extending paths on recursive descent works well in this script notice this script' reusable copyfile function--just in case there are multigigabyte files in the tree to be copiedit uses file' size to decide whether it should be read all at once or in chunks (rememberthe file read method without arguments actually loads the entire file into an in-memory stringwe choose fairly large file and block sizesbecause the more we read at once in pythonthe faster our scripts will typically run this is more efficient than it may soundstrings left behind by prior reads will be garbage collected and reused as we go we're using binary file modes here againtooto suppress the unicode encodings and end-of-line translations of text files--trees may contain arbitrary kinds of files also notice that this script creates the "todirectory if neededbut it assumes that the directory is empty when copy starts upfor accuracybe sure to remove the target directory before copying new tree to its nameor old files may linger in the target tree (we could automatically remove the target firstbut this may not always be desiredthis script also tries to determine if the source and target are the sameon unix-like platforms with oddities such as linksos path samefile does more accurate job than comparing absolute file names (different file names may be the same filehere is copy of big book examples tree ( use the tree from the prior edition throughout this in action on windowspass in the name of the "fromand "todirectories to kick off the processredirect the output to file if there are too many error messages to read all at once ( output txt)and run an rm - or rmdir / shell command (or similar platform-specific toolto delete the target directory first if neededc:\pp \system\filetoolsrmdir / copytemp copytempare you sure ( / ) :\pp \system\filetoolscpall py :\temp\pp \examples copytemp notedirto was created copying copied files directories in seconds :\pp \system\filetoolsfc / copytemp\pp \launcher py :\temp\pp \examples\pp \launcher py comparing files copytemp\pp \launcher py and :\temp\pp \examples\pp \launcher py fcno differences encountered copying directory trees