-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipding.py
48 lines (35 loc) · 1.22 KB
/
zipding.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
# import required modules
import os
import zipfile
# Declare the function to return all file paths of the particular directory
def retrieve_file_paths(dirName):
# setup file paths variable
filePaths = []
# Read all directory, subdirectories and file lists
for root, directories, files in os.walk(dirName):
for filename in files:
# Create the full filepath by using os module.
filePath = os.path.join(root, filename)
filePaths.append(filePath)
# return all paths
return filePaths
# Declare the main function
def main():
# Assign the name of the directory to zip
dir_name = 'images'
# Call the function to retrieve all files and folders of the assigned directory
filePaths = retrieve_file_paths(dir_name)
# printing the list of all files to be zipped
print 'The following list of files will be zipped:'
for fileName in filePaths:
print(fileName)
# writing files to a zipfile
zip_file = zipfile.ZipFile(dir_name+'.zip', 'w')
with zip_file:
# writing each file one by one
for file in filePaths:
zip_file.write(file)
print dir_name+'.zip file is created successfully!'
# Call the main function
if __name__ == "__main__":
main()