From f148f6af8afabed76a1f0749f2857e3dae7e264e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Sun, 18 Dec 2022 08:38:15 -0300 Subject: [PATCH] os + shutil - Copiando arquivos com Python --- aula173.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 aula173.py diff --git a/aula173.py b/aula173.py new file mode 100644 index 0000000..1fbe3d5 --- /dev/null +++ b/aula173.py @@ -0,0 +1,26 @@ +# os + shutil - Copiando arquivos com Python +# Vamos copiar arquivos de uma pasta para outra. +# Copiar -> shutil.copy +import os +import shutil + +HOME = os.path.expanduser('~') +DESKTOP = os.path.join(HOME, 'Desktop') +PASTA_ORIGINAL = os.path.join(DESKTOP, 'EXEMPLO') +NOVA_PASTA = os.path.join(DESKTOP, 'NOVA_PASTA') + +os.makedirs(NOVA_PASTA, exist_ok=True) + +for root, dirs, files in os.walk(PASTA_ORIGINAL): + for dir_ in dirs: + caminnho_novo_diretorio = os.path.join( + root.replace(PASTA_ORIGINAL, NOVA_PASTA), dir_ + ) + os.makedirs(caminnho_novo_diretorio, exist_ok=True) + + for file in files: + caminho_arquivo = os.path.join(root, file) + caminnho_novo_arquivo = os.path.join( + root.replace(PASTA_ORIGINAL, NOVA_PASTA), file + ) + shutil.copy(caminho_arquivo, caminnho_novo_arquivo)