copiar varios archivos en python


Cómo copiar todos los archivos de un directorio a otro en python. Tengo la ruta de origen y la ruta de destino en una cadena.

Author: hidayat, 2010-08-03

5 answers

Puede usar os.listdir () para obtener los archivos en el directorio fuente, os.camino.isfile () para ver si son archivos regulares (incluyendo enlaces simbólicos en sistemas *nix), y shutil.copy para hacer la copia.

El siguiente código copia solo los archivos normales del directorio de origen en el directorio de destino (asumo que no desea copiar ningún subdirectorio).

import os
import shutil
src_files = os.listdir(src)
for file_name in src_files:
    full_file_name = os.path.join(src, file_name)
    if (os.path.isfile(full_file_name)):
        shutil.copy(full_file_name, dest)
 86
Author: GreenMatt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-03 20:14:02

Si no desea copiar el árbol entero (con subdirectorios etc), el uso o glob.glob("path/to/dir/*.*") para obtener una lista de todos los nombres de archivo, recorrer la lista y utilice shutil.copy copiar cada archivo.

for filename in glob.glob(os.path.join(source_dir, '*.*')):
    shutil.copy(filename, dest_dir)
 20
Author: Steven,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-03 15:56:32

Mira shutil en los documentos de Python, específicamente el comando copytree.

 8
Author: Doon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-29 17:45:13
def recursive_copy_files(source_path, destination_path, override=False):
"""
Recursive copies files from source  to destination directory.
:param source_path: source directory
:param destination_path: destination directory
:param override if True all files will be overridden otherwise skip if file exist
:return: count of copied files
"""
files_count = 0
if not os.path.exists(destination_path):
    os.mkdir(destination_path)
items = glob.glob(source_path + '/*')
for item in items:
    if os.path.isdir(item):
        path = os.path.join(destination_path, item.split('/')[-1])
        files_count += recursive_copy_files(source_path=item, destination_path=path, override=override)
    else:
        file = os.path.join(destination_path, item.split('/')[-1])
        if not os.path.exists(file) or override:
            shutil.copyfile(item, file)
            files_count += 1
return files_count
 3
Author: Константин Бушко,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-18 23:12:36
import os
import shutil
os.chdir('C:\\') #Make sure you add your source and destination path below

dir_src = ("C:\\foooo\\")
dir_dst = ("C:\\toooo\\")

for filename in os.listdir(dir_src):
    if filename.endswith('.txt'):
        shutil.copy( dir_src + filename, dir_dst)
    print(filename)
 0
Author: Isaac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-23 00:08:14