Python
/
Storage
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Storage Zip
import zipfile fw = zipfile.ZipFile(DIR + '/data/archive.zip', 'w') Multiple files into archive
Zip
p237 With zip you can package multiple files in a single archive file.
"""Zip arhive
Compressing multiple files into a single archive file.
"""
import zipfile, os
DIR = os.path.dirname(os.path.realpath(__file__))
newZip = zipfile.ZipFile(DIR + '/data/archive.zip', 'w')
newZip.write(DIR + '/data/file1.txt', 'file1')
newZip.write(DIR + '/data/file2.txt', 'file2')
newZip.close()
archive = zipfile.ZipFile(DIR + '/data/archive.zip')
print(archive.namelist())
# file1, file2
info = archive.getinfo('file1')
print(info.file_size)
# 3
archive.close()
Extract
Extract all files into the working directory.
"""Extract from zip file.
Extract all files into the current directory.
"""
import zipfile, os
from pathlib import Path
DIR = Path(__file__).resolve().parent
with zipfile.ZipFile(DIR / 'data/archive.zip') as archive:
archive.extractall(DIR / 'data/extracted')
for root, dirs, files in os.walk(DIR / 'data/extracted'):
for name in files:
print(name)
# file1
# file2
➥ Questions