1. Completely copy a directory tree under another directory
Import OS, sys "" "Copy Directory Tree" "" maxloadsize = 1024x768 * 1024def copyfile (Frompath, Topath, maxloadsize= maxloadsize): "" "Single File Copy:p Aram Frompath: Original file:p Aram Topath: File copied to:p Aram Maxloadsize: Copy largest block: return: "" "Fromfilename = OS.P Ath.split (Frompath) [1] (Dirpath,filename) = Os.path.split (topath) if fromfilename! = Filename:topath = OS.PA Th.join (Dirpath, fromfilename) print ("changed copy file name:" + topath) if Os.path.getsize (frompath) <= Maxl Oadsize:with Open (Frompath, "RB") as Fromfile:with open (Topath, "WB") as Tofile:bytes = Fromfile.read (maxloadsize) tofile.write (bytes) Else:with open (Frompath, "RB") as FromFile: With open (Topath, "WB") as Tofile:while true:bytes = Fromfile.read (maxloadsize If not bytes:break tofile.write (bytes) def copytree (Dirfrom, dirto, verbose = 0): If not Os.path.isdir (dIrto): Os.mkdir (dirto) fount = Tcount =0 for filename in Os.listdir (dirfrom): Pathfrom = Os.path.join ( Dirfrom, filename) Pathto = os.path.join (dirto, filename) if not os.path.isfile (pathfrom): # directory Loop Copy Try:os.mkdir (pathfrom) FC,TC = Copytree (Pathfrom, Pathto) fo UNT + = FC Tcount + = TC Except:print ("Error copying from {0} to {1}". Format (PATHF Rom, pathto)) Else: # File Direct copy try:print ("Copy from {0} to {1}". Format (PATHFR OM, Pathto)) CopyFile (Pathfrom, pathto) fount + = 1 Except:print (" Error copying from {0} to {1} ". Format (Pathfrom, Pathto)) return (Fount, tcount) if __name__ = = ' __main__ ': #copyfile ( ".. /testdir1/test1.pdf ",". /testdir/testfdf.pdf ") Copytree (".. /lession6 ",". /less ")
Python Applet copy directory tree