Questions tagged [tarfile]

either a tar archive file or a Python module used to handle tar archive files

The term "tar file" can refer to either an archive file created with the UNIX tar command (also called a tarball) or the Python tarfile module that is used to read, write, and manage these files.

Resources

Related Tags

231 questions
173
votes
10 answers

How to create full compressed tar file using Python?

How can I create a .tar.gz file with compression in Python?
shahjapan
  • 13,637
  • 22
  • 74
  • 104
75
votes
5 answers

Size of an open file object

Is there a way to find the size of a file object that is currently open? Specifically, I am working with the tarfile module to create tarfiles, but I don't want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like…
strider1551
  • 763
  • 1
  • 5
  • 6
43
votes
2 answers

How to construct a TarFile object in memory from byte buffer in Python 3?

Is it possible to create a TarFile object in memory using a buffer containing the tar data without having to write the TarFile to disk and open it up again? We get the bytes sent over a socket. Something like this: import tarfile byte_array =…
Sefu
  • 2,404
  • 8
  • 42
  • 59
30
votes
4 answers

Safely extract zip or tar using Python

I'm trying to extract user-submitted zip and tar files to a directory. The documentation for zipfile's extractall method (similarly with tarfile's extractall) states that it's possible for paths to be absolute or contain .. paths that go outside the…
jterrace
  • 64,866
  • 22
  • 157
  • 202
16
votes
2 answers

How to unpack xz file with python which contains only data but no filename?

I have a file, which I can decompress under linux using the following command: unxz < file.xz > file.txt How can I do the same using python? If I use python3 and the tarfile module and do the following: import sys import tarfile try: with…
MiB_Coder
  • 865
  • 1
  • 7
  • 20
16
votes
2 answers

How to append a file to a tar file use python tarfile module?

I want to append a file to the tar file. For example, the files in test.tar.gz are a.png, b.png, c.png. I have a new png file named a.png, I want to append to a.png to test.tar.gz and cover the old file a.png in test.tar.gz. My code: import…
Karl Doenitz
  • 2,220
  • 3
  • 20
  • 38
12
votes
2 answers

Overwrite existing read-only files when using Python's tarfile

I'm attempting to use Python's tarfile module to extract a tar.gz archive. I'd like the extraction to overwrite any target files it they already exist - this is tarfile's normal behaviour. However, I'm hitting a snitch in that some of the files have…
victorhooi
  • 16,775
  • 22
  • 90
  • 113
10
votes
4 answers

How to determine if data is valid tar file without a file?

My upload form expects a tar file and I want to check whether the uploaded data is valid. The tarfile module supports is_tarfile(), but expects a filename - I don't want to waste resources writing the file to disk just to check if it is valid. Is…
hoju
  • 28,392
  • 37
  • 134
  • 178
9
votes
2 answers

Tarfile in Python: Can I untar more efficiently by extracting only some of the data?

I am ordering a huge pile landsat scenes from the USGS, which come as tar.gz archives. I am writing a simple python script to unpack them. Each archive contains 15 tiff images from 60-120 mb in size, totalling just over 2 gb. I can easily extract an…
Joe
  • 3,831
  • 4
  • 28
  • 44
8
votes
1 answer

How can I process a tarfile with a Python multiprocessing pool?

I'm trying to process the contents of a tarfile using multiprocessing.Pool. I'm able to successfully use the ThreadPool implementation within the multiprocessing module, but would like to be able to use processes instead of threads as it would…
Tim Whitcomb
  • 10,447
  • 3
  • 35
  • 47
8
votes
1 answer

Python tarfile and excludes

This is an excerpt from Python's documentation: If exclude is given it must be a function that takes one filename argument and returns a boolean value. Depending on this value the respective file is either excluded (True) or added (False). I…
Kaurin
  • 294
  • 1
  • 3
  • 9
7
votes
4 answers

Determine whether any files have been added, removed, or modified in a directory

I'm trying to write a Python script that will get the md5sum of all files in a directory (in Linux). Which I believe I have done in the code below. I want to be able to run this to make sure no files within the directory have changed, and no…
Greg
  • 45,306
  • 89
  • 231
  • 297
7
votes
2 answers

How to use Python3.6 tarfile module to read from memory?

I would like to download a tarfile from url to memory and than extract all its content to folder dst. What should I do? Below are my attempts but I could not achieve my plan. #!/usr/bin/python3.6 # -*- coding: utf-8 -*- from pathlib import…
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
6
votes
1 answer

Does Python's `tarfile` module store the archives it's building in memory?

I'm working in a memory constrained environment where I need to make archives of SQL dumps. If I use python's built in tarfile module is the '.tar' file held in memory or written to disk as it's created? For instance, in the following code, if…
Chris W.
  • 37,583
  • 36
  • 99
  • 136
6
votes
2 answers

Does Python's tarfile.open need close()?

In the official python documentation of tarfile I don't see wether a tarfile created with tarfile.open('example.tar', 'r:*') should be closed once you don't need it anymore. In some other examples (e.g. here) you often see the tarfile not to be…
m13r
  • 2,458
  • 2
  • 29
  • 39
1
2 3
15 16