Copy file or directories recursively in Python

前端 未结 5 881
无人及你
无人及你 2020-11-27 12:17

Python seems to have functions for copying files (e.g. shutil.copy) and functions for copying directories (e.g. shutil.copytree) but I haven\'t fou

相关标签:
5条回答
  • 2020-11-27 12:27

    shutil.copy and shutil.copy2 are copying files.

    shutil.copytree copies a folder with all the files and all subfolders. shutil.copytree is using shutil.copy2 to copy the files.

    So the analog to cp -r you are saying is the shutil.copytree because cp -r targets and copies a folder and its files/subfolders like shutil.copytree. Without the -r cp copies files like shutil.copy and shutil.copy2 do.

    0 讨论(0)
  • 2020-11-27 12:35

    Unix cp doesn't 'support both directories and files':

    betelgeuse:tmp james$ cp source/ dest/
    cp: source/ is a directory (not copied).
    

    To make cp copy a directory, you have to manually tell cp that it's a directory, by using the '-r' flag.

    There is some disconnect here though - cp -r when passed a filename as the source will happily copy just the single file; copytree won't.

    0 讨论(0)
  • 2020-11-27 12:40

    The python shutil.copytree method its a mess. I've done one that works correctly:

    def copydirectorykut(src, dst):
        os.chdir(dst)
        list=os.listdir(src)
        nom= src+'.txt'
        fitx= open(nom, 'w')
    
        for item in list:
            fitx.write("%s\n" % item)
        fitx.close()
    
        f = open(nom,'r')
        for line in f.readlines():
            if "." in line:
                shutil.copy(src+'/'+line[:-1],dst+'/'+line[:-1])
            else:
                if not os.path.exists(dst+'/'+line[:-1]):
                    os.makedirs(dst+'/'+line[:-1])
                    copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
                copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
        f.close()
        os.remove(nom)
        os.chdir('..')
    
    0 讨论(0)
  • 2020-11-27 12:42

    To add on Tzot's and gns answers, here's an alternative way of copying files and folders recursively. (Python 3.X)

    import os, shutil
    
    root_src_dir = r'C:\MyMusic'    #Path/Location of the source directory
    root_dst_dir = 'D:MusicBackUp'  #Path to the destination folder
    
    for src_dir, dirs, files in os.walk(root_src_dir):
        dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)
        for file_ in files:
            src_file = os.path.join(src_dir, file_)
            dst_file = os.path.join(dst_dir, file_)
            if os.path.exists(dst_file):
                os.remove(dst_file)
            shutil.copy(src_file, dst_dir)
    

    Should it be your first time and you have no idea how to copy files and folders recursively, I hope this helps.

    0 讨论(0)
  • 2020-11-27 12:45

    I suggest you first call shutil.copytree, and if an exception is thrown, then retry with shutil.copy.

    import shutil, errno
    
    def copyanything(src, dst):
        try:
            shutil.copytree(src, dst)
        except OSError as exc: # python >2.5
            if exc.errno == errno.ENOTDIR:
                shutil.copy(src, dst)
            else: raise
    
    0 讨论(0)
提交回复
热议问题