-
Notifications
You must be signed in to change notification settings - Fork 4k
/
mouredev.py
51 lines (30 loc) · 1.17 KB
/
mouredev.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import zipfile
import os
def zip_file(path: str, file: str) -> str:
file_path = os.path.join(path, file)
if os.path.exists(file_path):
zip_file = f"{file}.zip"
zip_path = os.path.join(path, zip_file)
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
zipf.write(file_path, file)
print(f"Archivo {file} comprimido como {zip_file}.")
return zip_file
else:
print(f"El archivo {file} no existe el directorio {path}.")
return None
def unzip_file(path: str, file: str):
file_path = os.path.join(path, file)
if os.path.exists(file_path):
zip_path = os.path.join(path, file)
with zipfile.ZipFile(zip_path) as zipf:
zipf.extractall(path)
print(f"Archivo {file} descomprimido en {path}.")
else:
print(f"El archivo {file} no existe el directorio {path}.")
path = os.path.dirname(os.path.abspath(__file__)) # Directorio actual
file = "nombre_de_mi_archivo"
zip = zip_file(path, file)
if zip != None:
# Borro el fichero original antes de descomprimir el zip
os.remove(os.path.join(path, file))
unzip_file(path, zip)