TypeError: Can't convert 'bytes' object to str implicitly using Python3.5

316
April 16, 2018, at 00:34 AM

I am using Python 3.5 on the following code.

def raxml(DIR,cleaned,num_cores,seqtype):
        assert cleaned.endswith(".aln-cln"),\
                "raxml infile "+cleaned+" not ends with .aln-cln"
        assert seqtype == "aa" or seqtype == "dna","Input data type: dna or aa"
        assert len(read_fasta_file(DIR+cleaned)) >= 4,\
                "less than 4 sequences in "+DIR+cleaned
        clusterID = cleaned.split(".")[0]
        tree = DIR+clusterID+".raxml.tre"
        raw_tree = "RAxML_bestTree."+cleaned
        model = "PROTCATWAG" if seqtype == "aa" else "GTRCAT"
        if not os.path.exists(tree) and not os.path.exists(raw_tree):
                # raxml crashes if input file starts with . 
                infasta = cleaned if DIR == "./" else DIR+cleaned
                cmd = ["raxml","-T",str(num_cores),"-p","12345","-s",\
                       infasta,"-n",cleaned,"-m",model]
                print (" ".join(cmd))
                p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
                out = p.communicate()
                assert p.returncode == 0,"Error raxml"+out[0]
        try:
                os.rename(raw_tree,tree)
                os.remove("RAxML_info."+cleaned)
                os.remove("RAxML_log."+cleaned)
                os.remove("RAxML_parsimonyTree."+cleaned)
                os.remove("RAxML_result."+cleaned)
                os.remove(DIR+cleaned+".reduced")
        except: pass # no need to worry about extra intermediate files
        return tree

It runs and returns the following code:

"raxml_wrapper.py", line 30, in raxml
        assert p.returncode == 0,"Error raxml"+out[0]
TypeError: Can't convert 'bytes' object to str implicitly

Initially, I tried the following:

 p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
 p = p.decode('utf-8')
 out = p.communicate()
 assert p.returncode == 0,"Error raxml"+out[0]

That didn't fix the issue at all. I have looked at similar questions, but I cannot come up with a solution to this. I would appreciate some help on this.

Thanks!

Answer 1

p, a Popen object, doesn't have a .decode(...) member.

You need to actually decode the output

p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, _ = p.communicate()
out = out.decode('utf-8')
assert p.returncode == 0, 'Error raxml' + out[0]

That said, this code can be improved to use subprocess.check_output:

# does roughly the same thing, you'll get `subprocess.CalledProcessError` instead of `AssertionError`
out = subprocess.check_output(cmd).decode('UTF-8')

Or if you happen to be using python3.6+

out = subprocess.check_output(cmd, encoding='UTF-8')
Answer 2

I do not know exactly what your p.communicate() method does, but it seems that it returns a byte object as a result. And this piece of code, cannot add this byte object to "Error raxml" str object:

assert p.returncode == 0,"Error raxml"+out[0]

Maybe you should try converting it to str as this:

assert p.returncode == 0,"Error raxml"+str(out[0])
Rent Charter Buses Company
READ ALSO
Unable to access OneDrive from Python

Unable to access OneDrive from Python

Similar questions have been asked, but I still can not get my simple python application to work

206
Python full keywords list

Python full keywords list

I know there is a keywords list called kwlist in python\Lib\keywordpy but only the basic keywords are there like if print and is and so on

265
New to python. Pip SSL error on Mac

New to python. Pip SSL error on Mac

I keep getting this SSL error when using pip on my macI have done a fresh install of the OS and it hasn't made any different

204