简体   繁体   中英

Python3: f.read() in Tarfile returns bytes instead of a file-like object and it passes empty file

I am having issue with passing file-like object of a tgz file in Python. here is how my code looks like:

backup = tarfile.open(backup_file, mode='r:gz')
for f in backup.getmembers():
    if f.name.endswith('.xml'):
        ff = f.name
        backupff = backup.extractfile(ff)
        if backupff:
            backupobj = backupff.read()
backup.close()

The problem arises from

backupobj = backupff.read()

and it gives this error:

AttributeError: 'bytes' object has no attribute 'read'

I don't have such a problem when dealing with zip files.

Update

@AKX, you are right that this not the code I'm running. The real code are very big and I am not sure anyone has time to look into it.

Anyway, when I run the main function, I receive this error:

file_read = file.read

AttributeError: 'bytes' object has no attribute 'read'

Here is file.read part:

def sendfile(self, file, offset=0, count=None):
        """Borrowed from https://github.com/python/cpython/blob/3.6/Lib/socket.py
        and adapted to our needs
        """
        self._check_sendfile_params(file, offset, count)
        if self.request.gettimeout() == 0:
            raise ValueError("non-blocking sockets are not supported")
        if offset:
            file.seek(offset)
        blocksize = min(count, 8192) if count else 8192
        total_sent = 0
        # localize variable access to minimize overhead
        file_read = file.read
        sock_send = self.request.send

I'm quite positive that's not the code you're running, or not the environment you're running in. I can't reproduce this with Python 3.8:

$ echo aaa > 1.xml
$ echo bbb > 2.xml
$ tar czvf a.tar.gz *.xml
a 1.xml
a 2.xml
$ cat x.py
import tarfile

backup_file = 'a.tar.gz'

with tarfile.open(backup_file, mode='r:gz') as backup:
    for member in backup.getmembers():
        if member.name.endswith('.xml'):
            fh = backup.extractfile(member)
            if fh:
                content = fh.read()
                print((member.name, content))
$ python3 x.py
('1.xml', b'aaa\n')
('2.xml', b'bbb\n')

I found the solution:

There is no need to use following commands:

if backupff:
   backupobj = backupff.read()

This line is sufficient to assign the file to external user

backupff = backup.extractfile(ff)

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