Problems with "Tarfile.close()"
Barry
barry at barrys-emacs.org
Sat Dec 21 04:58:42 EST 2019
> On 20 Dec 2019, at 15:31, Dr Rainer Woitok <rainer.woitok at gmail.com> wrote:
>
> Greetings,
>
> One of my Python scripts basically does the following:
>
> source = tarfile.open(name=tar_archive , mode='r|*')
> dest = tarfile.open(fileobj=sys.stdout, mode='w|', format=fmt)
>
> .
> .
> .
>
> source.close()
> dest.close()
>
> In an attempt to move my Python scripts from Python 2.7 to Python 3.6 I
> ran into the problem that under Python 3.6 the call to "dest.close()"
> fails:
>
> Traceback (most recent call last):
> File ".../tar_archive.copy", line 137, in <module>
> dest.close()
> File "/usr/lib64/python3.6/tarfile.py", line 1742, in close
> self.fileobj.close()
> File "/usr/lib64/python3.6/tarfile.py", line 467, in close
> self.fileobj.write(self.buf)
> TypeError: write() argument must be str, not bytes
>
> What am I doing wrong? By the way: since on some hosts this script is
> running on the transition from Python 2.7 to Python 3.x will not happen
> immediately, I need a solution which works with both versions.
Stdout on python 3 is a text stream. Tarfiles need a binary stream.
I searched for “python 3 binary stdout” and found this
stack over flow question that has suggested fixes.
https://stackoverflow.com/questions/908331/how-to-write-binary-data-to-stdout-in-python-3#908440
Barry
>
> Sincerely,
> Rainer
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list