Python – How to resolve child processes that contain ‘|’

How to resolve child processes that contain ‘|’… here is a solution to the problem.

How to resolve child processes that contain ‘|’

This code is invalid.
That’s how I wrote.

str = "curl -s 'URL_ADDRESS' | tail -1".split()
p = subprocess. Popen(str,stdout=subprocess. PIPE).stdout
data = p.read()
p.close()
print(data)

But the result is b''.
What’s wrong with that?

Solution

If you use child processes, use instead of ‘|’ Like this.

This will solve the problem.

str = "curl -s 'URL_ADDRESS'".split()

tail = "tail -1".split()

temp = subprocess. Popen(str, stdout=subprocess. PIPE).stdout

temp1 = subprocess. Popen(tail, stdin=temp, stdout=subprocess. PIPE).stdout

temp.close()

data = temp1.read()

temp1.close()

Related Problems and Solutions