Python: ¿cuál es la forma más rápida de copiar archivos desde una carpeta de origen a varios destinos?

source = /tmp/src contains a,b,c,d files destinations = '/one' , '/two'

so i want to copy files a,b,c,d to both destinations '/one' and 'two'

algo como

source = '/tmp/src'
destinations = []

def copy_files_multiple_dest(source,destinations)

right ? now, how would i loop through all the destinations

preguntado el 01 de febrero de 12 a las 22:02

Will the files ever need to diverge? You might get aware with hardlinking if you're 100% sure they will always be the same after the copy. -

The source stays the same , since its populated by 'apt-get install' and whatever is in the repo folders, which is all packaged up in a .deb file -

4 Respuestas

que tal algo como:

import os
import shutil

source = '/tmp/src/'
destinations = []

def copy_files_multiple_dest(source,destinations):
  sfiles = os.listdir(source) # list of all files in source
  for f in sfiles:
    for dest in destinations:
      shutil.copy(os.path.join(source,f), dest)

i'm not sure it's the más rápido but it should do the job.

Respondido 02 Feb 12, 02:02

i get error[Errno 21] Is a directory: '/tmp/1/', when destinations = ['/tmp/1/','/tmp/2/'], code is appended to yours above - kamal

@kamal as the error message says, you're trying to move a directory. you can add a condition before the move function: if os.path.isfile(dest):.... - yurib

i had to use .strip() since argparse only supports strings and not lists. In my code, i was saving the space delimeted directory PATHs in a list - kamal

Reading the source files only once makes sense here:

def xcopy_to_multiple_destinations(srcDir, destinations):
    for filename in os.listdir(srcDir):
        with open(os.path.join(srcDir, filename), "rb") as srcFile:
            for destDir in destinations:
                with open(os.path.join(destDir, filename), "wb") as destFile:
                    # ...copy bytes from srcFile to destFile...

If you want to copy recursively, use os.walk (see other question: Lectura de carpeta recursiva de Python). You can adapt the solution accordingly.

Note that "fastest" is a broad term. Hard linking should be faster, for example ;) Or using copy-on-write with the appropriate file system.

contestado el 23 de mayo de 17 a las 15:05

os package is the usual way to got but have a look at this new project you could then just do:

import pbs
destinations =['/one', '/two']
for destination in destinations:
   pbs.copy("-R", '/tmp/src', destination)

maybe not the fastest but certainly wins the beauty contest

Respondido 02 Feb 12, 05:02

Since you say the files won't diverge, you can just hardlink them. You don't need python specifically, here's how I'd do it in bash.

dests=(a b c d)
for dest in "${dests[@]}"; do
  cp -rl /source/root "$dest"

If it has to be python, look at and other functions in that module.

Respondido 03 Feb 12, 00:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.