[python] Compress and decompress

Thing you want to do

--Compress all files in one directory into another --Extract the compressed file to the original directory

point

At the time of powershell, I used 7zip to operate from the command line, but I wondered if there was anything else When I looked it up, there was something very useful

-** shutil ** is used as import --Compression: ** `shutil.make_archive``` ** [compression destination path], [format], [path of the directory you want to compress] -** Extension is not included in [** compression destination path **] ** --Unzip: ** `shutil.unpack_archive``` ** [path you want to unzip], [destination] -** Add the extension to [** Path you want to unzip **] **

sample

compression


import shutil

#Compression destination
done_dir    = 'C:/test/done/testzip'
#Directory of files you want to compress
output_dir = 'C:/test/output/'
z = done_dir
r = output_dir
#compression
shutil.make_archive(z, 'zip', root_dir=r)

Defrost


#Compressed file you want to decompress
zf = f'{z}.zip'
#Defrost
shutil.unpack_archive(zf, extract_dir=output_dir)

Recommended Posts

[python] Compress and decompress
Compress / decompress tarfile
Compress python data and write to sqlite
Python and numpy tips
[Python] pip and wheel
Batch design and python
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
python input and output
Python and Ruby split
Archive and compress the entire directory with python
Python3, venv and Ansible
Python asyncio and ContextVar
Programming with Python and Tkinter
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo
Python installation and basic grammar
I compared Java and Python!
Python shallow and deep copy
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Python environment construction and TensorFlow
Python class variables and instance variables
Ruby and Python syntax ~ branch ~
[Python] Python and security-① What is Python?
Stack and Queue in Python
python metaclass and sqlalchemy declareative
Fibonacci and prime implementations (python)
Python basics: conditions and iterations
Decompress multiple compressed files (Python)
Python bitwise operator and OR
Python debug and test module
Python list and tuples and commas
Python variables and object IDs
Python list comprehensions and generators
About Python and regular expressions
python with pyenv and venv
Unittest and CI in Python
Maxout description and implementation (Python)
[python] Get quotient and remainder
Python 3 sorted and comparison functions
[Python] Depth-first search and breadth-first search
Identity and equivalence Python is and ==