-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.py
48 lines (36 loc) · 1.24 KB
/
zip.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
# importing required modules
from zipfile import ZipFile
import os
from datetime import date
hoje = date.today()
dia = hoje.strftime("%d-%m-%Y")
pasta = f'E:/UNILAB/{dia}'
def get_all_file_paths(directory):
# initializing empty file paths list
file_paths = []
# crawling through directory and subdirectories
for root, directories, files in os.walk(directory):
for filename in files:
# join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath)
# returning all file paths
return file_paths
def main():
# path to folder which needs to be zipped
directory = f'{pasta}'
# calling function to get all file paths in the directory
file_paths = get_all_file_paths(directory)
# printing the list of all files to be zipped
print('Following files will be zipped:')
for file_name in file_paths:
print(file_name)
# writing files to a zipfile
os.chdir(pasta)
with ZipFile('my_python_files.zip', 'w') as zip:
# writing each file one by one
for file in file_paths:
zip.write(file)
print('All files zipped successfully!')
if __name__ == "__main__":
main()