简体   繁体   中英

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:\Users\dzhao\Desktop\123456\444444\blah.py", line 15, in <module>
    shutil.copyfileobj(source, target)
  File "C:\Python34\lib\shutil.py", line 67, in copyfileobj
    buf = fsrc.read(length)
  File "C:\Python34\lib\gzip.py", line 365, in read
    if not self._read(readsize):
  File "C:\Python34\lib\gzip.py", line 433, in _read
    if not self._read_gzip_header():
  File "C:\Python34\lib\gzip.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?

fileobj isn't a documented property of TarFile . It's probably an internal object used to represent the whole tar file, not something specific to the current file.

Use TarFile.extractfile() to get a file-like object for a specific member:

…
source = tar.extractfile(member)
target = open("out", "wb")
shutil.copyfile(source, target)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM