<p><br>
On Dec 26, 2012 11:00 AM, "Antoon Pardon" <<a href="mailto:antoon.pardon@rece.vub.ac.be">antoon.pardon@rece.vub.ac.be</a>> wrote:<br>
><br>
> I am converting some programs to python 3. These programs manipulate tarfiles. In order for the python3 programs to be really useful<br>
> they need to be able to process the tarfiles produced by python2 that however seems to be a problem.<br>
><br>
> This is testcode that produces a tarfile.<br>
><br>
> #! /usr/bin/python<br>
><br>
> compression = "bz2"<br>
> tarmode = "w|%s" % compression<br>
> rt = '.'<br>
><br>
> import os<br>
> import os.path<br>
> import errno<br>
><br>
> import tarfile as tar<br>
><br>
> def process():<br>
>     pj = os.path.join<br>
>     entries = os.listdir(rt)<br>
>     of = open("DUMP.tbz", "w")<br>
>     tf = tar.open(mode = tarmode, fileobj = of,<br>
>                   encoding = 'ascii', format = tar.PAX_FORMAT)<br>
>     for entry in entries:<br>
>         fqpn = pj(rt, entry)<br>
>         try:<br>
>             tf.add(fqpn, entry, recursive = False)<br>
>         except OSError as ErrInfo:<br>
>             print("%s: disappeared" % fqpn)<br>
>             if ErrInfo.errno != errno.ENOENT:<br>
>                 raise<br>
>     tf.close()<br>
>     of.close()<br>
><br>
> if __name__ == "__main__":<br>
>     process()<br>
><br>
> ==============================================================================<br>
> This is testcode that checks a tarfile<br>
><br>
> #!/usr/bin/python<br>
><br>
> compression = "bz2"<br>
> tarmode = "r|%s" % compression<br>
><br>
> import os<br>
> import os.path<br>
> import stat<br>
><br>
> import tarfile as tar<br>
><br>
> def equalfile(fl1, fl2):<br>
>     bf1 = fl1.read(8192)<br>
>     bf2 = fl2.read(8192)<br>
>     while bf1 == bf2:<br>
>         if bf1 == "":<br>
>             return True<br>
>         bf1 = fl1.read(8192)<br>
>         bf2 = fl2.read(8192)<br>
>     return False<br>
><br>
> def process():<br>
>     gf = open("DUMP.tbz", "r")<br>
>     tf = tar.open(mode = tarmode, fileobj = gf,<br>
>                   encoding = 'ascii', format = tar.PAX_FORMAT)<br>
>     for tarinfo in tf:<br>
>         entry = <a href="http://tarinfo.name">tarinfo.name</a><br>
>         fileinfo = os.stat(entry)<br>
>         if stat.S_ISREG(fileinfo.st_mode) and tarinfo.isreg():<br>
>             bfl = tf.extractfile(tarinfo)<br>
>             ofl = open(entry)<br>
>             if not equalfile(bfl, ofl):<br>
>                 print("%s: does not match backup" % entry)<br>
>                 sync = False<br>
>     tf.close()<br>
>     gf.close()<br>
><br>
> if __name__ == "__main__":<br>
>     process()<br>
><br>
> =================================================================================<br>
><br>
> When I use python2.7 to produce and later check the tarfile everything works as expected. However when I use python3.2 to check the tarfile I<br>
> get the following traceback.<br>
><br>
> Traceback (most recent call last):<br>
>   File "tarchck", line 39, in <module><br>
>     process()<br>
>   File "tarchck", line 25, in process<br>
>     encoding = 'ascii', format = tar.PAX_FORMAT)<br>
>   File "/usr/lib/python3.2/tarfile.py", line 1771, in open<br>
>     t = cls(name, filemode, stream, **kwargs)<br>
>   File "/usr/lib/python3.2/tarfile.py", line 1667, in __init__<br>
>     self.firstmember = self.next()<br>
>   File "/usr/lib/python3.2/tarfile.py", line 2418, in next<br>
>     tarinfo = self.tarinfo.fromtarfile(self)<br>
>   File "/usr/lib/python3.2/tarfile.py", line 1281, in fromtarfile<br>
>     buf = tarfile.fileobj.read(BLOCKSIZE)<br>
>   File "/usr/lib/python3.2/tarfile.py", line 573, in read<br>
>     buf = self._read(size)<br>
>   File "/usr/lib/python3.2/tarfile.py", line 585, in _read<br>
>     buf = self.__read(self.bufsize)<br>
>   File "/usr/lib/python3.2/tarfile.py", line 604, in __read<br>
>     buf = self.fileobj.read(self.bufsize)<br>
>   File "/usr/lib/python3.2/codecs.py", line 300, in decode<br>
>     (result, consumed) = self._buffer_decode(data, self.errors, final)<br>
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9e in position 10: invalid start byte<br>
><br>
> I have been looking around but have no idea how I have to adapt this code in order to have it process the tarfile under python3.2. The original code didn't have the coding and format keywords on the tar.open statement and after reading the documentation I thought that<br>

> would make things work, but no such luck. Further reading didn't<br>
> provide anything usefull<br>
><br>
> -- <br>
> Antoon Pardon<br>
> -- <br></p>
<p>You're opening the file in text mode, so it's trying to decode it as text using your default encoding (utf-8). You want the file read as a series of bytes, so open it in binary mode.</p>
<p>gf =open("DUMP.tbz", "rb")<br>
</p>