SoFunction
Updated on 2024-11-14

How python implements copying a directory to a specified directory

This article example shares the specific code for python copy directory to a specified directory for your reference, the details are as follows

Save the following code as a file and run it directly

import os
import time
copyFileCounts = 0
def copyFiles(sourceDir, targetDir):
 global copyFileCounts
 print (sourceDir)
 print (u"%s Current Processing Folder %s Processed %s Files" %(('%Y-%m-%d %H:%M:%S',(())), sourceDir,copyFileCounts))
 for f in (sourceDir):
  sourceF = (sourceDir, f)
  targetF = (targetDir, f)
  if (sourceF):
   #Creating a Catalog
   if not (targetDir):
    (targetDir)
   copyFileCounts += 1
   # files do not exist, or do exist but are different sizes, overwrite the
   if not (targetF) or ((targetF) and ((targetF) != (sourceF))):
    #Binary files
    open(targetF, "wb").write(open(sourceF, "rb").read())
    print (u"%s %s Copy complete" %(('%Y-%m-%d %H:%M:%S',(())), targetF))
   else:
    print (u"%s %s already exists, not duplicated" %(('%Y-%m-%d %H:%M:%S',(())), targetF))
  if (sourceF):
   copyFiles(sourceF, targetF)
if __name__ == "__main__":
 copyFiles('/content/chest_xray/', '/content/drive/My Drive/chest_xray/')

Delete all files in a non-empty directory, including the directory itself.

import shutil
('D:/content/')

This is the whole content of this article.