How to create full compressed tar file using Python?
To build a .tar.gz
(aka .tgz
) for an entire directory tree:
import tarfile
import os.path
def make_tarfile(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
This will create a gzipped tar archive containing a single top-level folder with the same name and contents as source_dir
.
Creating tar file in Python with tarfile makes directory tree inside of tar.gz
Below code from below link solves my question.
How to create full compressed tar file using Python?
import tarfile
import os.path
def make_tarfile(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
Giving absolute address to the params output_filename and source_dir works appropriate
Create .tar.gz archive on Windows 10 using Python 3.6 and tarfile module
This will create a .tar.gz file with a single file in the same directory as the original file, only suffixed with .tar.gz
. Maybe that's what you're looking for?
import tarfile
original_path = "C:\\Myfolder\\MyArchive.txt"
tgz_path = original_path + ".tar.gz"
with tarfile.open(tgz_path, "w:gz") as tar:
tar.add(original_path)
Python: create a compressed tar file for streamed writing
Here's an example of how to write a compressed tarfile from a Python script:
import StringIO
import tarfile
tar = tarfile.open('example.tar.gz', 'w:gz')
# create a file record
data = StringIO.StringIO('this is some text')
info = tar.tarinfo()
info.name = 'foo.txt'
info.uname = 'pat'
info.gname = 'users'
info.size = data.len
# add the file to the tar and close it
tar.addfile(info, data)
tar.close()
Result:
% tar tvf example.tar.gz
-rw-r--r-- 0 pat users 17 Dec 31 1969 foo.txt
Compressing only the files inside a given directory using tarfile (Python)
First, you are using parameter recursive
wrongly.
According to the official document of tarfile
:
def add(self, name, arcname=None, recursive=True, exclude=None):
"""Add the file `name' to the archive. `name' may be any type of file
(directory, fifo, symbolic link, etc.). If given, `arcname'
specifies an alternative name for the file in the archive.
Directories are added recursively by default. This can be avoided by
setting `recursive' to False. `exclude' is a function that should
return True for each filename to be excluded.
"""
You can use arcname
to specify the alternative name in the archive. And recursive
is used to control if creates directories recursively.
tarfile
can directly add a directory.
Back to your question, you can manually add each files and specify their arcname
. For example, tar.add("updates/package1/file1", "file1")
.
update
Or you can set arcname
to an empty string. As it will omit the root directory.
Related Topics
Conda Command Is Not Recognized on Windows 10
How to Get the "Id" After Insert into MySQL Database with Python
How to Make a 4D Plot with Matplotlib Using Arbitrary Data
Parameter Substitution for a SQLite "In" Clause
Multiple Inputs and Outputs in Python Subprocess Communicate
How to Insert Newlines on Argparse Help Text
In Python, How to Index a List with Another List
Thread Safety in Python's Dictionary
How to Run an Ipython Magic from a Script (Or Timing a Python Script)
Too Many Different Python Versions on My System and Causing Problems
Sqlalchemy Create_All() Does Not Create Tables
Python Extending with - Using Super() Python 3 VS Python 2
Matplotlib Axes.Plot() VS Pyplot.Plot()
Play an Animated Gif in Python with Tkinter