Jackie
Jackie

Reputation: 23529

coercing to Unicode: need string or buffer, TarFile found

I get the following error....

Traceback (most recent call last):
  File "deploycommerce.py", line 56, in <module>
    if tarfile.is_tarfile(optfile):
  File "/usr/lib/python2.7/tarfile.py", line 2587, in is_tarfile
    t = open(name)
  File "/usr/lib/python2.7/tarfile.py", line 1658, in open
    return func(name, "r", fileobj, **kwargs)
  File "/usr/lib/python2.7/tarfile.py", line 1720, in gzopen
    fileobj = bltn_open(name, mode + "b")
TypeError: coercing to Unicode: need string or buffer, TarFile found

when I try to fun the following...

optfile = tarfile.open(opt_tar_input,"r:gz")
# ERROR THROWN IN FOLLOWING...
if tarfile.is_tarfile(optfile):
    # extract all contents
    test =""

thanks guys

Upvotes: 1

Views: 1243

Answers (2)

Katriel
Katriel

Reputation: 123662

tarfile.is_tarfile takes the name of a file, not the file object.

If you've successfully called tarfile.open then the path pointed to a tarfile.

Note that the usual Python coding style would be

try:
    optfile = tarfile.open(...)
except tarfile.ReadError:
    # not a tarfile

This is usually summarised in the slogan "it's easier to ask forgiveness than permission".

Upvotes: 4

Cat Plus Plus
Cat Plus Plus

Reputation: 129814

tarfile.is_tarfile takes a file name, not TarFile object. The check is redundant anyway —if it wasn't a tar file, tarfile.open would raise an exception.

Upvotes: 0

Related Questions