4 examples of 'python zip folder' in Python

Every line of 'python zip folder' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
179def _PythonUnzip(self):
180 """Unzip a file using the python zipfile module."""
181 ziplocal = None
182 try:
183 ziplocal = zipfile.ZipFile(self._zipfile_name)
184 ziplocal.extractall()
185 finally:
186 if ziplocal is not None:
187 ziplocal.close()
19def zipdir(path, ziph):
20 # ziph is zipfile handle
21 for root, dirs, files in os.walk(path):
22 for filename in files:
23 file = os.path.join(root, filename)
24 destination = os.path.relpath(file, path)
25 if extra:
26 destination = os.path.join(extra, destination)
27 print("Zipping " + file)
28 if (filename in executables):
29 f = open(file, 'r')
30 bytes = f.read()
31 f.close()
32
33 info = zipfile.ZipInfo(destination)
34 info.date_time = time.localtime()
35 #info.external_attr |= 0o755 <<
36 info.external_attr = 0o100777 << 16
37 print("Execute permissions set for " + file)
38
39 ziph.writestr(info, bytes, zipfile.ZIP_DEFLATED)
40 else:
41 ziph.write(file, destination)
18def zipdir(path, zip):
19 if path.endswith('/'):
20 path = path[:-1]
21 for root, dirs, files in os.walk(path, followlinks=True):
22 for file in files:
23 p = os.path.join(root, file)
24 name = p[len(path)+1:]
25 if excludes.match(name):
26 continue
27 zip.write(p, name)
38def zip_dir(path: str):
39 zipf = zipfile.ZipFile(f'{path}.zip', 'w', zipfile.ZIP_DEFLATED)
40
41 for root, dirs, files in os.walk(path):
42 for file in files:
43 zipf.write(os.path.join(root, file))
44
45 zipf.close()

Related snippets