vendredi 10 juin 2016

How do I to turn my .tar.gz file into a file-like object for shutil.copyfileobj?


My goal is to extract a file out of a .tar.gz file without also extracting out the sub directories that precede the desired file. I am trying to module my method off this question. I already asked a question of my own but it seemed like the answer I thought would work didn't work fully.

In short, shutil.copyfileobj isn't copying the contents of my file.

My code is now:

import os
import shutil
import tarfile
import gzip

with tarfile.open('RTLog_20150425T152948.gz', 'r:*') as tar:
    for member in tar.getmembers():
        filename = os.path.basename(member.name)
        if not filename:
            continue

        source = tar.fileobj
        target = open('out', "wb")
        shutil.copyfileobj(source, target)

Upon running this code the file out was successfully created however, the file was empty. I know that this file I wanted to extract does, in fact, have lots of information (approximately 450 kb). A print(member.size) returns 1564197.

My attempts to solve this were unsuccessful. A print(type(tar.fileobj)) told me that tar.fileobj is a <gzip _io.BufferedReader name='RTLog_20150425T152948.gz' 0x3669710>.

Therefore I tried changing source to: source = gzip.open(tar.fileobj) but this raised the following error:

Traceback (most recent call last):
  File "C:UsersdzhaoDesktop123456444444blah.py", line 15, in <module>
    shutil.copyfileobj(source, target)
  File "C:Python34libshutil.py", line 67, in copyfileobj
    buf = fsrc.read(length)
  File "C:Python34libgzip.py", line 365, in read
    if not self._read(readsize):
  File "C:Python34libgzip.py", line 433, in _read
    if not self._read_gzip_header():
  File "C:Python34libgzip.py", line 297, in _read_gzip_header
    raise OSError('Not a gzipped file')
OSError: Not a gzipped file

Why isn't shutil.copyfileobj actually copying the contents of the file in the .tar.gz?


Aucun commentaire:

Enregistrer un commentaire