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)