Problems with "Tarfile.close()"

Dr Rainer Woitok rainer.woitok at gmail.com
Fri Dec 20 07:19:26 EST 2019


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.

Sincerely,
  Rainer


More information about the Python-list mailing list