Archive and compress the entire directory with python

Introduction

I wanted to make a backup, so on Google [python + directory + compression] search for. Note that Zubari did not hit the top of the search.

Compress directory

tar_dir.py


import tarfile

#Directory name
dir_name = 'data'
#Compressed file name
tar_name = dir_name + '.tar.gz'

#Compression process
archive = tarfile.open(tar_name, mode='w:gz')
archive.add(dir_name)
archive.close()

If you specify a directory, you can compress the directory.

Directory structure

Such directories can be compressed.   data/    ├ mydir/    │ ├ nestdir/    │ │ ├ aaa.txt    │ │ ├ bbb.txt    │ │ └ foo.csv    │ ├ bar.csv    │ ├ ccc.txt    │ └ ddd.txt    ├ eee.txt    ├ fff.txt    └ hoge.csv

Mode list

The tarfile has the following modes. (Investigated with Python 3.5)

Grammar 1: filemode Grammar 2: filemode [: compression] (for random seek) Grammar 3: filemode [| compression] (for streams) Default: r

mode action
'r'Or'r:*' Transparent for compression method, open for reading
'r:' Uncompressed and open for reading
'r:gz' Open for reading with gzip compression
'r:bz2' Open for reading with bzip2 compression
'r:xz' Open for reading with lzma compression
'x'Or'x:' Create tarfile exclusively without compression.FileExistsError if the file exists
'x:gz' Create tarfile with gzip compression.FileExistsError if the file exists
'x:bz2' Create tarfile with bzip2 compression.FileExistsError if the file exists
'x:xz' Create tarfile with lzma compression.FileExistsError if the file exists
'a'Or'a:' Uncompressed and open for appending.If the file does not exist, create a new one
'w'Or'w:' Uncompressed and open for writing
'w:gz' Open for writing with gzip compression
'w:bz2' Open for writing with bzip2 compression
'w:xz' Open for writing with lzma compression
'r|*' Transparently open stream for reading regarding compression method
'r|' Open uncompressed stream for reading
'r|gz' Open gzip compressed stream for reading
'r|bz2' Open bzip2 compressed stream for reading
'r|xz' Open lzma compressed stream for reading
'w|' Open uncompressed stream for writing
'w|gz' open stream for writing
'w|bz2' open stream for writing
'w|xz' open stream for writing

Command line

You can archive and compress directories from the command line.

$ python -m tarfile -c tar_name.tar.gz dir_name/

It switches the compression method according to the extension of the tar file.

extension Compression method
.gz gz
.tgz gz
.xz xz
.txz xz
.bz2 bz2
.tbz bz2
.tbz2 bz2
.tb2 bz2

List of command line options for tarfile

option フルoption argument Remarks
-v --verbose Default is False
-l --list <tarfile>
-e --extract <tarfile> <output_dir>
-c --create <name> <file>
-t --test <tarfile>

Reference information

Original document tarfile (link)

Recommended Posts

Archive and compress the entire directory with python
[python] Compress and decompress
Solving the Lorenz 96 model with Julia and Python
Visualize the range of interpolation and extrapolation with python
Install the latest stable Python with pyenv (both 2 and 3)
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Call the API with python3.
python with pyenv and venv
Create a directory with python
Get home directory with python
Works with Python and R
Automate background removal for the latest portraits in a directory with Python and API
Try hitting the Twitter API quickly and easily with Python
[Python] Read the csv file and display the figure with matplotlib
Solve the spiral book (algorithm and data structure) with python!
[Python] Get the script execution directory with an absolute path
Play with Mastodon's archive in Python 2 Count replies and favourites
Play with the password mechanism of GitHub Webhook and Python
Shuffle the images in any directory with Python and save them in another folder with serial numbers.
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
[Python] Check the current directory, move the directory
Extract the xz file with python
The story of Python and the story of NaN
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python and Beautiful Soup
Get the weather with Python requests 2
Find the Levenshtein Distance with python
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2
Reading and writing NetCDF with Python
I liked the tweet with python. ..
I played with PyQt5 and Python3
Master the type with Python [Python 3.9 compatible]
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Command for the current directory Python
Solve the Python knapsack problem with a branch and bound method
Operate Firefox with Selenium from python and save the screen capture
What should I do with the Python directory structure after all?
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
Communicate between Elixir and Python with gRPC
Make the Python console covered with UNKO
Data pipeline construction with Python and Luigi
Fill the string with zeros in python and count some characters from the string
Calculate and display standard weight with python
I replaced the numerical calculation of Python with Rust and compared the speed
Monitor Mojo outages with Python and Skype