-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress_folder_and_files.py
More file actions
37 lines (32 loc) · 1.1 KB
/
Copy pathCompress_folder_and_files.py
File metadata and controls
37 lines (32 loc) · 1.1 KB
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
import zipfile
import sys
import os
def zip_file(file_path):
compress_file = zipfile.ZipFile(file_path+'.zip','w')
compress_file.write(path,compress_type=zipfile.ZIP_DEFLATED)
compress_file.close()
def retrieve_file_paths(dir_name):
file_paths=[]
for root,directories,files in os.walk(dir_name):
for filename in files:
file_path = os.path.join(root,filename)
file_paths.append(file_path)
return file_paths
def zip_dir(dir_path,file_paths):
compress_dir = zipfile.ZipFile(dir_path + '.zip','w')
with compress_dir:
for file in file_paths:
compress_dir.write(file)
if __name__=="__main__":
path=sys.argv[1]
if os.path.isdir(path):
files_path = retrievve_file_paths(path)
print("The following list of files will be zipped:")
for file_name in files_path:
print(file_name)
zip_dir(path,files_path)
elif os.path.isfile(path):
print("The %s will be zipped:"%path)
zip_file(path)
else:
print('a special file(socket,FIFO,device file),please input file or dir')