Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatenate str
#1
I have a main class that returns the result of a secondary class. The problem is defined in the following message in the terminal:

TypeError: can only concatenate str (not "CompletedProcess") to str

This is due to the sub-process that I need to concatenate within the body of the html, below is an excerpt from the primary class and the secondary class:

Primary Class

def do_GET(self):
    self.do_HEAD()
    from index import Index
    o_html = Index().view()
    self.wfile.write(bytes(o_html, "utf8"))
    return
Secondary Class

#! /usr/local/bin/python3.9
import subprocess

# Class name : Index -> Structure html page :
class Index:

    # Class method : Constructor -> Object Build :
    def __init__(self):
        pass

    # Class method : build ->  : Render code html :
    def view(self):

        self.v_shell = subprocess.run("ls")
        # Variable : v_html5 -> Code Html
        self.v_html5 = """
            <!DOCTYPE html>
            <html lang="pt-BR">
            <head>
                <meta charset="UTF-8">
                <meta http-equiv="X-UA-Compatible" content="IE=edge">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Webstrucs Action</title>
            </head>
            <body>
                <h1>Action</h1>
                Python Web Server For Asynchronous Requests<br>
                """ + self.v_shell + """
                <p>
                </p>
            </body>
            </html>
        """
        return self.v_html5
How can I get around this TypeError?
Reply
#2
You expect subprocess.run("ls") returns a string, but in "Subprocess management" I read:
... Wait for command to complete, then return a CompletedProcess instance
A CompletedProcess instance contains: args, returncode, stdout and stderr.

Therefore I believe you should do: subprocess.run("ls").stdout.
Reply
#3
This is how it works :

subprocess.getoutput("ls")
ibreeden likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate two dataframes moralear27 2 1,883 Sep-15-2020, 08:04 AM
Last Post: moralear27
  can only concatenate str (not "int") to str gr3yali3n 6 4,123 May-28-2020, 07:20 AM
Last Post: pyzyx3qwerty
  Concatenate two dictionaries harish 3 2,373 Oct-12-2019, 04:52 PM
Last Post: strngr12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020