-
Notifications
You must be signed in to change notification settings - Fork 0
/
aula174.py
33 lines (26 loc) · 1.14 KB
/
aula174.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# os + shutil - Apagando, copiando, movendo e renomeando pastas com Python
# Vamos copiar arquivos de uma pasta para outra.
# Copiar -> shutil.copy
# Copiar Árvore recursivamente -> shutil.copytree
# Apagar Árvore recursivamente -> shutil.rmtree
# Apagar arquivos -> os.unlink
# Renomear/Mover -> shutil.move ou os.rename
import os
import shutil
HOME = os.path.expanduser('~') #pega home do usuario
PATH_DOWNLOAD = os.path.join(HOME, 'Downloads')
ORIGINAL_FOLDER = os.path.join(PATH_DOWNLOAD, 'exemplo_teste_aula')
NEW_FOLDER = os.path.join(PATH_DOWNLOAD, 'exemplo02')
shutil.rmtree(NEW_FOLDER)
shutil.copytree(ORIGINAL_FOLDER, NEW_FOLDER)
# os.makedirs(NEW_FOLDER, exist_ok=True)
# for root, dirs, files in os.walk(ORIGINAL_FOLDER):
# for dir_ in dirs: # cria as subpastas
# new_path_directory = os.path.join(root.replace(
# ORIGINAL_FOLDER, NEW_FOLDER), dir_
# )
# os.makedirs(new_path_directory, exist_ok=True)
# for file in files:
# path_file = os.path.join(root, file)
# new_path_file = os.path.join(root.replace(ORIGINAL_FOLDER, NEW_FOLDER), file)
# shutil.copy(path_file, new_path_file)